flutter_compass icon indicating copy to clipboard operation
flutter_compass copied to clipboard

Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0

Open kw2019ltd opened this issue 5 years ago • 1 comments

hi i see this message in console

W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 D/ViewRootImplInjector(22907): Gesture Screenshot Enabled = false W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 I/chatty (22907): uid=10669(kw.ltd.salatkapp) identical 5 lines W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 I/chatty (22907): uid=10669(kw.ltd.salatkapp) identical 11 lines W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 I/chatty (22907): uid=10669(kw.ltd.salatkapp) identical 3 lines W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0 W/FlutterJNI(22907): Tried to send a platform message to Flutter, but FlutterJNI was detached from native C++. Could not send. Channel: hemanthraj/flutter_compass. Response ID: 0

kw2019ltd avatar Jun 12 '20 06:06 kw2019ltd

Here's what I did to solve this problem:

I looked at the "sensors" flutter package and mimicked their coding pattern.

I added my own flutter compass code in my_flutter_compass.dart file

import 'dart:async';
import 'package:flutter/services.dart';

const EventChannel _compassEventChannel =
    EventChannel('hemanthraj/flutter_compass');    

Stream<double> _compassEvents;

/// A broadcast stream of events from the compass
Stream<double> get compassEvents {
  if (_compassEvents == null) {
    _compassEvents = _compassEventChannel
        .receiveBroadcastStream()
        .map<double>((dynamic data) => data);
  }
  return _compassEvents;
}

I added the following variable to my stateful widget:

StreamSubscription<double> _eventListener; I set the variable in my initState() function:

  @override
  void initState() {
    super.initState();

    _eventListener = compassEvents.listen((angle) {
      setState(() {
        _angle = angle.floor().toString();
      });
    });
  }

And I canceled the _eventListener in 2 separate places:

  @override
  Widget build(BuildContext context) {
    return WillPopScope(
      onWillPop: () async {
        _eventListener?.cancel();
        _eventListener = null;
        return true;
      },
      child: Scaffold(
        body: Compass(),
      ),
    );
  }

  @override
  void dispose() {
    super.dispose();
    _eventListener?.cancel();    
  }

I had to do it also in onWillPop event because dispose() seemed not to be called if my compass screen was the main screen of the application.

This also solved the problem where the sensor stopped working after closing and starting the app a few times.

I hope this helps others who are having the same problem.

postacik avatar Aug 01 '20 11:08 postacik