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));
}

2

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.