2

I am trying to create an Isolate un Flutter and then use this isolate to fetch some data from Firebase Realtime Database.

I am creating de Isolate in a file called home.dart (not main) and here is my code for that file. I have a class to create the Isolate and the function for the Isolate to execute. Inside this function I am trying to fetch the data.

void elIsolate(SendPort sPort) async {


  print("Fetching data");

  final databaseReference = FirebaseDatabase.instance.reference().child("categories");
  DataSnapshot info;
  /*databaseReference.once().then((DataSnapshot snapshot) {
    info = snapshot;
    print(info.value);

  });*/

  print("new isolate created");

  IsolateChannel channel = IsolateChannel.connectSend(sPort);
  channel.stream.listen((data) {
    print('newIsolate received : $data');
  });
  channel.sink.add("hi");
}

class _MyHomePageState extends State<MyHomePage> {

  List list = [];

  void initState(){
    WidgetsFlutterBinding.ensureInitialized();
    super.initState();
    print("Init state");

    loadIsolate();
  }

  Future loadIsolate() async {
    await Firebase.initializeApp();

    print("Load isolate");

    ReceivePort rPort =  ReceivePort();
    IsolateChannel channel = IsolateChannel.connectReceive(rPort);

    channel.stream.listen((data) {
      print('rootIsolate received : $data');
      channel.sink.add('How are you');
    });

    await Isolate.spawn(elIsolate, rPort.sendPort);

    /*await Isolate.spawn(getAllWorkers, receivePort.sendPort);
    receivePort.listen((message) {
      print(message);
    });*/

  }
} 

Then I have my main.dart. I added this line inside the main function: WidgetsFlutterBinding.ensureInitialized();

Here is my code

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  await Firebase.initializeApp();
  bool resp;
  await SharedPreferences.getInstance().then((prefs) {
    resp = prefs.getBool('isUser');
    if (resp == null) {
      FirebaseAuth _auth = FirebaseAuth.instance;
      resp = (_auth.currentUser != null);
      prefs.setBool('isUser', resp);
    }
  });
  runApp(MyApp(user: resp));
}

  • 1
    This seems to be your question, it's a known issue [Unable to call a platform channel method from another isolate #13937](https://github.com/flutter/flutter/issues/13937) – Spike L May 12 '21 at 01:50
  • 1
    Does this answer your question? [Flutter: Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized](https://stackoverflow.com/questions/57689492/flutter-unhandled-exception-servicesbinding-defaultbinarymessenger-was-accesse) – croxx5f May 12 '21 at 01:51

1 Answers1

0
flutter_isolate: ^2.0.2

onPressed: () {
  FlutterIsolate.spawn(_isolateEntrypoint, "");
}
    
// A "top level" function (i.e. not inside a class or make it static)
_isolateEntrypoint(String foo) {
   WidgetsFlutterBinding.ensureInitialized();
   ...
}

Make sure that authorization and initialization were made on the same main thread (top level or static).

Now this FlutterEngine will be able to communicate with Firebase Realtime Database but the main FlutterEngine won't. In practice, depending on the app, an app may want to communicate with Realtime Database from either engine (or both). In background apps, more likely from here rather than the main isolate, but again that depends on the app.