flutter_foreground_task icon indicating copy to clipboard operation
flutter_foreground_task copied to clipboard

[Android] Sending custom object through SendPort makes app crash - Release only

Open GaelleJoubert opened this issue 2 years ago • 6 comments

I've been using this library for a while now, but the last Version created a new bug for me. I've mannaged to find a workaround in the way i'm using the library to avoid that crash, but I though I'd open an issue, because this is still a bug worth fixing.

A bit of context : Platform : Android (no bug in IOS), reproduced in Android 10, 12 & 13 Phone : Reproduced on Pixel 6A, crosscall X4, Samsung A12

Note that this issue is happening ONLY in RELEASE, in debug I have no issue.

The issue :

To communicate between the Forground Service and the App, I'm using SendPort.send et Received port, like in the exemple. When I use these port to send Data like Int, String or Map I have no issue.

BUT I was using them also to send a simple custom Object that I wrote, which is basically a simple class with different string and int attribute. And when I send this object via the SendPort, the App Crash.

It uses to work fine with the previous version of the library, and it is still working fine in Debug.

With a lot of time and print, I have made sure that the crash is only related to the object being sent through the send port. Indeed :

  • If I just send a String -> No crash
  • If I send my CustomObject be with nothing on the side of the received port except for a print -> crash

In the Documentation about sendPorts, I saw the following :

Sends an asynchronous message through this send port, to its corresponding ReceivePort.
The transitive object graph of message can contain the following objects:
Null
bool
int
double
String
List or Map (whose elements are any of these)
TransferableTypedData
SendPort
Capability
If the sender and receiver isolate share the same code (e.g. isolates created via Isolate.spawn), the transitive object graph of message can contain any object,

So what I guess is that the isolates in this library are now handled diffrently and do not "share the same code", and that's why sending my custom object makes the app crash, but if I send something else, it does not.

Maybe this is because of the new "pragma" we need to add ?

My Workaround Instead of sending my CustomObject, I created a method who transform it into a Map, with it's Attributes as Elements of this map. Something like :

Map _map = Map();
_map ["Attribute1"] = CustomObject.Attribute1 ;
_map ["Attribute2"] = CustomObject.Attribute2 ;
_map ["Attribute3"] = CustomObject.Attribute3 ;

And then I send this Map instead of My Object. Then when I received this map in the Received Port, I can build my Object Again.

With this Workaround my code works fine, but I think it would be nice to have the SendPort to work again like it use to. Does anyone notice the same thing ? Does anyone know how to fix it ?

How to Reproduce the Issue :

1- Create a custom class with some random attributes :

class CustomObject{
int attribute1 ;
String attribute2 ;

CustomObject(attribute1, attribute2);
}

2- In your Foreground task somewhere, send it through the SendPort :

sendPort?.send(CustomObject(1,"test"));

and eventually add in your ReceivedPort a methode to handle t hat data or just to print something when any data is received.

3- Notice your app crash

GaelleJoubert avatar Oct 04 '22 15:10 GaelleJoubert

any update ?

vinhbui1 avatar Oct 31 '22 03:10 vinhbui1

On my side, I am still using the workaround I mentionned. Do you notice this behaviour as well ?

GaelleJoubert avatar Nov 02 '22 08:11 GaelleJoubert

I have the same issue .Thanks you. you helped me saved a few day on this problem

buihongvinh avatar Nov 02 '22 09:11 buihongvinh

using map DS to send custom objects to prevent crash. it worked for me.

Faiizii avatar Jan 04 '23 05:01 Faiizii

Thanks you solved my issues.

Nagaraj-kumaravel avatar Jan 23 '23 05:01 Nagaraj-kumaravel

I confirm that sending a map solves the issue. But, it is still a bug... or would need to be documented...

nicolaspernoud avatar Jan 23 '23 14:01 nicolaspernoud