Cannot execute Firebase Query within Isolate
Asked Answered
I

1

7

I'm developping an app using Flutter. And I want to execute some Firebase queries using Isolate. But each time I run the app I get this error and nothing is displayed.

Here my code

class HomePage extends StatefulWidget {
    HomePage({Key key, this.title}) : super(key: key);
    final String title;

    @override
    HomePageState createState() => HomePageState();
 }


class HomePageState extends State<HomePage>{
    Isolate _isolate;
    ReceivePort _receivePort;
    String _data;

  @override
  Widget build(BuildContext context){
    return WillPopScope(
    child: Scaffold(){
     child : Center(child:Text('${_data}'))
     });
  }


 //To start
  void _start() async {
  _receivePort = ReceivePort();
  _isolate = await Isolate.spawn(getData, _receivePort.sendPort);
  _receivePort.listen(_displayData, onDone: () {
     print("done!");
  });
}

 //Display data;
void _displayData(dynamic data) {
  setState(() {
    _data = data;
  });
 }

  static void getData(SendPort sendPort) async{
   var fire = Firestore.instance;
   fire.settings(persistenceEnabled: true); //I get an error here
   fire.document('MODEL/${id}')
    .snapshots()
    .listen((d) {//I get an error here
     sendPort.send(d);
    }
 }
}

 //I call the method _start in the initState
@override
 void initState() {
    _start();
   super.initState();
  }

Here is the error that I get

E/flutter (14817): [ERROR:flutter/runtime/dart_isolate.cc(808)] Unhandled exception: E/flutter (14817): error: native function 'Window_sendPlatformMessage' (4 arguments) cannot be found E/flutter (14817): #0 Window.sendPlatformMessage (dart:ui/window.dart:1133:9) E/flutter (14817): #1 _DefaultBinaryMessenger._sendPlatformMessage (package:flutter/src/services/binary_messenger.dart:85:15) E/flutter (14817): #2 _DefaultBinaryMessenger.send (package:flutter/src/services/binary_messenger.dart:129:12) E/flutter (14817): #3 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:309:51) E/flutter (14817): E/flutter (14817): #4 Query.snapshots. (package:cloud_firestore/src/query.dart:61:37) E/flutter (14817): #5 _runGuarded (dart:async/stream_controller.dart:805:24) E/flutter (14817): #6 _BroadcastStreamController._subscribe (dart:async/broadcast_stream_controller.dart:213:7) E/flutter (14817): #7 _ControllerStream._createSubscription (dart:async/stream_controller.dart:818:19) E/flutter (14817): E/flutter (14817): #4 Firestore.settings (package:cloud_firestore/src/firestore.dart:154:19) E/flutter (14817): E/flutter (14817): #5 HomePageState.getData (package:flutter_app/HelpFile/HomePage.dart:207:10) E/flutter (14817): #8 _StreamImpl.listen (dart:async/stream_impl.dart:472:9) E/flutter (14817): #9 HomePageState.getData (package:flutter_app/HomePage.dart:201:10) E/flutter (14817): E/flutter (14817): #10 _startIsolate. (dart:isolate-patch/isolate_patch.dart:308:17) E/flutter (14817): #11 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:172:12)

Ignorant answered 30/11, 2019 at 21:16 Comment(3)
Which platform are you using? What is _getData function?Litta
I use Android for my test and Windows 8.1 for developping. I wrote _getData instead of getData.Ignorant
Did you find any solutions to this problem?Kirksey
A
2

Check this link https://firebase.google.com/docs/reference/android/com/google/firebase/FirebaseApp

Any FirebaseApp initialization must occur only in the main process of the app. Use of Firebase in processes other than the main process is not supported and will likely cause problems related to resource contention.

Adaiha answered 22/10, 2020 at 13:23 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.