Skip to content

FlutterJNI was detached from native C++ #14

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
darshansatra1 opened this issue Aug 15, 2021 · 3 comments
Closed

FlutterJNI was detached from native C++ #14

darshansatra1 opened this issue Aug 15, 2021 · 3 comments

Comments

@darshansatra1
Copy link

Error: W/FlutterJNI(31092): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: step_count. Response ID: 0

When I closed the app, sometimes while walking the debug console shows me this message. Can you help me with this, why I'm getting this error?

@Dev-hwang
Copy link
Owner

@darshansatra1

This error occurs when you subscribe to a stream and do not unsubscribe from the stream when exiting the app. Be sure to call the cancel function in the onDestroy function.

NOTE: Code to get a stream like Geolocator.getPositionStream() should be called inside the callback function. The same goes for the positionStream variable. Do not try to use this with main isolate by declaring it as a global variable or static.

void startCallback() {
  final positionStream = Geolocator.getPositionStream();
  StreamSubscription<Position>? streamSubscription;

  FlutterForegroundTask.initDispatcher((timestamp, sendPort) async {
    if (streamSubscription != null) return;

    streamSubscription = positionStream.listen((event) {
      print('timestamp: ${timestamp.toString()}');

      FlutterForegroundTask.update(
          notificationTitle: 'Current Position',
          notificationText: '${event.latitude}, ${event.longitude}');
    });
  }, onDestroy: (timestamp) async {
    await streamSubscription?.cancel();  // here
    print('Dispatcher is dead.. x_x');
  });
}

The implementation process is as follows:

  1. Upgrade flutter_foreground_task: ^2.1.0
  2. Create and subscribe to the stream inside the callback as in the example above. Don't try to use the same stream for background isolate and main isolate.
  3. Check How to use-3 in the README file.
  4. When the FlutterForegroundTask.start() function is called successfully, it returns a ReceivePort object. Subscribe to ReceivePort.
  5. Send data to main isolate using sendPort.send provided by FlutterForegroundTask.initDispatcher.
  6. Implement the ReceivePort you subscribed to in step 4 in more detail. Update your UI or process data here.
  7. Be sure to call receivePort?.close() when the app is closed.
  8. When the app restarts, call FlutterForegroundTask.stop() and go back to step 4.

@darshansatra1
Copy link
Author

Yess I followed the instructions and now it's working awesome.

@behradkhadem
Copy link

As it looks initDispacher has been removed. IDE gives me this:

The method 'initDispatcher' isn't defined for the type 'FlutterForegroundTask'.
Try correcting the name to the name of an existing method, or defining a method named 'initDispatcher'.

What is the alternative version of the code above for flutter_foreground_task: ^3.5.5 ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants