Repository navigation
_send is somehow destroying additional field in Computer #4
Description
Activity
- changed the title
[-]_send is somehow destroying addition fiel in Computer[/-][+]_send is somehow destroying additional field in Computer[/+]on Feb 28, 2024 @Ben1980 I think its an issue on how Isolates transfer objects.
You either have to make the
BluetoothDevicefinal or make sure it can be serialized.If this doesn't work I suggest caching the object on the isolate site and only passing an identifier, and retrieving from cached objects when the identifier is passed back.
Isolates in Dart can transfer objects between them, but with some limitations:
- Primitive data types and built-in objects: These types (e.g., integers, strings, lists, maps) are transferred directly without any modifications.
- Custom objects:
- Final fields: These fields are serialized and transferred to the receiving isolate, maintaining their values.
- Non-final fields: Due to serialization and deserialization during the transfer process, their values of final fields might not be preserved if they:
- Contain references to mutable objects: The references themselves are transferred, but the actual objects might not be, potentially leading to unexpected behavior.
- Implement custom serialization logic: If the class defines its own toJson or other serialization methods, those methods will be used, and the resulting representation might not include all non-final fields.
There are a few more limitations, read here.
- addedquestionFurther information is requestedFurther information is requested
on Feb 28, 2024 You're right, i think as well this is the issue. BluetoothDevice is a foreign class from flutter_blue_plus unfortunately so I will need to got the caching way. Do you have any hint or suggestion how to do it? Otherwise I would utilize shared_preferences.
@Ben1980 first you should try making the field
finalinComputer.
But if the object contains more non-final children this will not work.I wouldn't go with shared preferences or any persistent caches, but instead a normal memory cache.
First make sure the objects are comparable (hashCodeandequalityimplemented), then just create a HashMap with the objectshashCodeas key.
Forward the hashcode to the non-isolate layer, and when getting it back try to read the object back from the memory cache.How would you do that for a foreign class of another plugin like BluetoothDevice? As far as I can see it is not providing a proper
hashCodenorequalityfunction. I've never used a memory cache, do you have any examples?@Ben1980 I think it does implement the required equality functions!
So what you can do now is the following:
final bluetoothDeviceCache = Map<int, BluetoothDevice>(); /// Function returns a hashCode which can be used to retrieve the BluetoothDevice int storeBluetoothDevice(BluetoothDevice device) { bluetoothDeviceCache[device.hashCode] = device; return device.hashCode; } /// Function returns a BluetoothDevice based on its hashCode. BluetoothDevice? getBluetoothDevice(int hashCode) { return bluetoothDeviceCache[hashCode]; }
now just pass the
hashCodeintreceved fromstoreBluetoothDeviceto the main layer and back to the isolate.Ok currently i don't know how to solve thisproblem. I tried passing
BluetoothDeviceto the isolate but tht seems not compatible because i don't get a proper object. I also tried the caching idea but also there i'm not sure how to go on because the isolate doesn't share memory. I think a call would help, you know more about flutter/dart so you probably know what to do.
I'm currently working on BLE support. Because of that, I needed to extend
Computerwith the additional fieldBluetoothDevice? device. The problem starts when calling download viadownload, afterward the object computer no longer contains a validBluetoothDeviceobject. All other fields are fine.