Open Ben1980 opened 8 months ago
@Ben1980 I think its an issue on how Isolates transfer objects.
You either have to make the BluetoothDevice
final 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:
Custom objects:
There are a few more limitations, read here.
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 final
in Computer
.
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 (hashCode
and equality
implemented), then just create a HashMap with the objects hashCode
as 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 hashCode
nor equality
function. 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 hashCode
int
receved from storeBluetoothDevice
to the main layer and back to the isolate.
Ok currently i don't know how to solve thisproblem. I tried passing BluetoothDevice
to 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
Computer
with the additional fieldBluetoothDevice? device
. The problem starts when calling download viadownload
, afterward the object computer no longer contains a validBluetoothDevice
object. All other fields are fine.