Skip to content

[firestore] Cannot use filestore on non-main isolates. #3124

Description

@ninyawee

It had been discuss #1815, here and here

To Reproduce
Steps to reproduce the behavior:

First, create a function that access firestore

Future runOneTime(String userId) async {
  final databaseReference = Firestore.instance;
  final users = databaseReference.collection('users');
  final files = databaseReference.collection('files');
}

Second call the code in an isolate

e.g.

Button(
                  text: "sync",
                  onPressed: () async {
                    await compute(runOneTime, userId);
                  },
                )

Error is printed

E/flutter (12393): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized.
E/flutter (12393): If you're running an application and need to access the binary messenger before `runApp()` has been called (for example, during plugin initialization), then you need to explicitly call the `WidgetsFlutterBinding.ensureInitialized()` first.
E/flutter (12393): If you're running a test, you can call the `TestWidgetsFlutterBinding.ensureInitialized()` as the first line in your test's `main()` method to initialize the binding.
E/flutter (12393): #0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
E/flutter (12393): #1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
E/flutter (12393): #2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
E/flutter (12393): #3      MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:375:5)
E/flutter (12393): #4      new MethodChannelFirestore (package:cloud_firestore_platform_interface/src/method_channel/method_channel_firestore.dart:27:13)
E/flutter (12393): #5      FirestorePlatform.instance (package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart:63:19)
E/flutter (12393): #6      new Firestore (package:cloud_firestore/src/firestore.dart:26:42)
E/flutter (12393): #7      Firestore.instance (package:cloud_firestore/src/firestore.dart:29:36)
E/flutter (12393): #8      runOneTime (package:gebwai/tasks/image_sync.dart:150:39)
E/flutter (12393): #9      _IsolateConfiguration.apply (package:flutter/src/foundation/_isolates_io.dart:75:34)
E/flutter (12393): #10     _spawn.<anonymous closure> (package:flutter/src/foundation/_isolates_io.dart:83:65)
E/flutter (12393): #11     Timeline.timeSync (dart:developer/timeline.dart:163:22)
E/flutter (12393): #12     _spawn (package:flutter/src/foundation/_isolates_io.dart:80:18)
E/flutter (12393): #13     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:304:17)
E/flutter (12393): #14     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:168:12)
E/flutter (12393): 

Expected behavior
Able to use firestore on any isolate

Additional context
Add any other context about the problem here.

Flutter doctor
Run flutter doctor and paste the output below:

Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, v1.17.5, on Microsoft Windows [Version 10.0.19042.423], locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK version 29.0.3)
    X Android license status unknown.
      Try re-installing or updating your Android SDK Manager.
      See https://developer.android.com/studio/#downloads or visit visit https://flutter.dev/docs/get-started/install/windows#android-setup for detailed instructions.
[!] Android Studio (version 3.6)
    X Unable to determine bundled Java version.
[√] Android Studio (version 4.0)
[√] VS Code (version 1.47.3)
[√] Connected device (1 available)

! Doctor found issues in 2 categories.

My flutter workflow is working properly, although the message suggests another.

I would like to ask. Is this intentional or a by-product of a singleton? Do you have any workarounds for the moment?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions