firebase / flutterfire

🔥 A collection of Firebase plugins for Flutter apps.
https://firebase.google.com/docs/flutter/setup
BSD 3-Clause "New" or "Revised" License
8.7k stars 3.97k forks source link

🐛 [firebase_core] Cannot initialize Firebase app in non-main isolate #4846

Closed awaik closed 10 months ago

awaik commented 3 years ago

Bug report

Hi! When we upload a lot of files it is good to make it in a separate isolate. But when we try to create a reference to the storage StorageReference reference = FirebaseStorage.instance.ref().child('file_name'); in the isolate we got the error

ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. 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. 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.

Steps to reproduce

Steps to reproduce the behavior:

  1. Create isolate.
  2. Use code StorageReference reference = FirebaseStorage.instance.ref().child('file_name');

Expected behavior

We should have the possibility to upload files to firebase storage in isolates.


Flutter doctor

Run flutter doctor and paste the output below:

Click To Expand ``` Doctor summary (to see all details, run flutter doctor -v): [✓] Flutter (Channel stable, 1.22.6, on macOS 11.1 20C69 darwin-x64, locale en-RU) [!] Android toolchain - develop for Android devices (Android SDK version 30.0.2) ✗ Android license status unknown. Run `flutter doctor --android-licenses` to accept the SDK licenses. See https://flutter.dev/docs/get-started/install/macos#android-setup for more details. [✓] Xcode - develop for iOS and macOS (Xcode 12.1) ```

Flutter dependencies

Run flutter pub deps -- --style=compact and paste the output below:

Click To Expand ``` Dart SDK 2.10.5 Flutter SDK 1.22.6 popcrn 1.0.50 |-- auto_size_text 2.1.0 | '-- flutter... |-- better_player 0.0.48 | |-- cupertino_icons... | |-- flutter... | |-- flutter_hls_parser 1.0.0 | | |-- collection... | | |-- flutter... | | |-- meta... | | '-- quiver... | |-- flutter_widget_from_html_core 0.5.1+4 | | |-- flutter... | | '-- html 0.14.0+4 | | |-- csslib 0.16.2 | | | '-- source_span... | | '-- source_span... | |-- meta... | |-- path_provider... | |-- pedantic... | |-- visibility_detector... | '-- wakelock 0.2.1+1 | |-- flutter... | |-- meta... | |-- wakelock_platform_interface 0.1.0+1 | | |-- flutter... | | '-- meta... | '-- wakelock_web 0.1.0+3 | |-- flutter... | |-- flutter_web_plugins... | |-- import_js_library 1.0.2 | | |-- flutter... | | |-- flutter_web_plugins... | | |-- html... | | '-- js... | |-- js... | '-- wakelock_platform_interface... |-- build_runner 1.10.3 | |-- args 1.6.0 | |-- async 2.5.0-nullsafety.1 | | '-- collection... | |-- build 1.5.0 | | |-- analyzer... | | |-- async... | | |-- convert... | | |-- crypto... | | |-- glob... | | |-- logging... | | |-- meta... | | '-- path... | |-- build_config 0.4.2 | | |-- checked_yaml 1.0.2 | | | |-- json_annotation... | | | |-- source_span... | | | '-- yaml... | | |-- json_annotation... | | |-- meta... | | |-- path... | | |-- pubspec_parse... | | '-- yaml... | |-- build_daemon 2.1.4 | | |-- built_collection 4.3.2 | | | |-- collection... | | | '-- quiver... | | |-- built_value 7.1.0 | | | |-- built_collection... | | | |-- collection... | | | |-- fixnum 0.10.11 | | | '-- quiver... | | |-- http_multi_server... | | |-- logging... | | |-- path... | | |-- pedantic... | | |-- pool... | | |-- shelf... | | |-- shelf_web_socket... | | |-- stream_transform... | | |-- watcher... | | '-- web_socket_channel... | |-- build_resolvers 1.4.2 | | |-- analyzer... | | |-- build... | | |-- crypto... | | |-- graphs... | | |-- logging... | | |-- meta... | | |-- package_config 1.9.3 | | | |-- charcode... | | | '-- path... | | |-- path... | | |-- pool... | | '-- pub_semver... | |-- build_runner_core 6.0.3 | | |-- async... | | |-- build... | | |-- build_config... | | |-- build_resolvers... | | |-- collection... | | |-- convert... | | |-- crypto... | | |-- glob... | | |-- graphs... | | |-- json_annotation... | | |-- logging... | | |-- meta... | | |-- package_config... | | |-- path... | | |-- pedantic... | | |-- pool... | | |-- timing... | | |-- watcher... | | '-- yaml... | |-- code_builder 3.5.0 | | |-- built_collection... | | |-- built_value... | | |-- collection... | | |-- matcher... | | '-- meta... | |-- collection... | |-- crypto... | |-- dart_style 1.3.8 | | |-- analyzer... | | |-- args... | | |-- path... | | '-- source_span... | |-- glob 1.2.0 | | |-- async... | | |-- collection... | | |-- node_io 1.1.1 | | | |-- node_interop 1.1.1 | | | | '-- js... | | | '-- path... | | |-- path... | | |-- pedantic... | | '-- string_scanner... | |-- graphs 0.2.0 | |-- http_multi_server 2.2.0 | | '-- async... | |-- io 0.3.4 | | |-- charcode... | | |-- meta... | | |-- path... | | '-- string_scanner... | |-- js 0.6.2 | |-- logging 0.11.4 | |-- meta... | |-- mime 0.9.7 | |-- path... | |-- pedantic... | |-- pool 1.4.0 | | |-- async... | | '-- stack_trace... | |-- pub_semver 1.4.4 | | '-- collection... | |-- pubspec_parse 0.1.5 | | |-- checked_yaml... | | |-- json_annotation... | | |-- pub_semver... | | '-- yaml... | |-- shelf 0.7.9 | | |-- async... | | |-- collection... | | |-- http_parser... | | |-- path... | | |-- stack_trace... | | '-- stream_channel... | |-- shelf_web_socket 0.2.3 | | |-- shelf... | | |-- stream_channel... | | '-- web_socket_channel... | |-- stack_trace 1.10.0-nullsafety.1 | | '-- path... | |-- stream_transform 1.2.0 | |-- timing 0.1.1+2 | | '-- json_annotation... | |-- watcher 0.9.7+15 | | |-- async... | | |-- path... | | '-- pedantic... | |-- web_socket_channel 1.1.0 | | |-- async... | | |-- crypto... | | '-- stream_channel... | '-- yaml 2.2.1 | |-- charcode... | |-- collection... | |-- source_span... | '-- string_scanner... |-- cached_network_image 2.3.3 | |-- flutter... | |-- flutter_cache_manager 2.0.0 | | |-- clock... | | |-- file 5.2.1 | | | |-- intl... | | | |-- meta... | | | '-- path... | | |-- flutter... | | |-- http... | | |-- path... | | |-- path_provider... | | |-- pedantic... | | |-- rxdart... | | |-- sqflite... | | '-- uuid 2.2.2 | | |-- convert... | | '-- crypto... | '-- octo_image 0.3.0 | |-- flutter... | '-- flutter_blurhash 0.5.0 | |-- flutter... | '-- meta... |-- cloud_firestore 0.14.4 | |-- cloud_firestore_platform_interface 2.2.1 | | |-- collection... | | |-- firebase_core... | | |-- flutter... | | |-- meta... | | '-- plugin_platform_interface... | |-- cloud_firestore_web 0.2.1+2 | | |-- cloud_firestore_platform_interface... | | |-- firebase_core... | | |-- firebase_core_web... | | |-- flutter... | | |-- flutter_web_plugins... | | |-- http_parser... | | |-- js... | | '-- meta... | |-- firebase_core... | |-- firebase_core_platform_interface... | |-- flutter... | |-- meta... | '-- quiver... |-- crypto 2.1.5 | |-- collection... | |-- convert 2.1.1 | | |-- charcode... | | '-- typed_data... | '-- typed_data... |-- cupertino_icons 1.0.0 |-- dots_indicator 1.2.0 | '-- flutter... |-- file_picker 2.1.5+1 | |-- flutter... | |-- flutter_plugin_android_lifecycle 1.0.11 | | '-- flutter... | |-- flutter_web_plugins 0.0.0 | | |-- characters... | | |-- collection... | | |-- flutter... | | |-- meta... | | |-- typed_data... | | '-- vector_math... | '-- plugin_platform_interface 1.0.3 | '-- meta... |-- firebase_auth 0.18.4+1 | |-- firebase_auth_platform_interface 2.1.4 | | |-- firebase_core... | | |-- flutter... | | |-- meta... | | '-- plugin_platform_interface... | |-- firebase_auth_web 0.3.2+3 | | |-- firebase_auth_platform_interface... | | |-- firebase_core... | | |-- firebase_core_web... | | |-- flutter... | | |-- flutter_web_plugins... | | |-- http_parser... | | |-- intl... | | |-- js... | | '-- meta... | |-- firebase_core... | |-- firebase_core_platform_interface... | |-- flutter... | '-- meta... |-- firebase_core 0.5.3 | |-- firebase_core_platform_interface 2.1.0 | | |-- flutter... | | |-- meta... | | |-- plugin_platform_interface... | | '-- quiver... | |-- firebase_core_web 0.2.1+1 | | |-- firebase_core_platform_interface... | | |-- flutter... | | |-- flutter_web_plugins... | | |-- js... | | '-- meta... | |-- flutter... | |-- meta... | '-- quiver 2.1.3 | |-- matcher... | '-- meta... |-- firebase_dynamic_links 0.6.3 | |-- firebase_core... | '-- flutter... |-- firebase_messaging 7.0.3 | |-- firebase_core... | |-- flutter... | '-- meta... |-- firebase_storage 4.0.1 | |-- firebase_core... | '-- flutter... |-- flushbar 1.10.4 | '-- flutter... |-- flutter 0.0.0 | |-- characters 1.1.0-nullsafety.3 | |-- collection 1.15.0-nullsafety.3 | |-- meta 1.3.0-nullsafety.3 | |-- sky_engine 0.0.99 | |-- typed_data 1.3.0-nullsafety.3 | | '-- collection... | '-- vector_math 2.1.0-nullsafety.3 |-- flutter_app_badger 1.1.2 | '-- flutter... |-- flutter_google_places 0.2.6 | |-- flutter... | |-- google_maps_webservice... | |-- http... | '-- rxdart 0.24.1 |-- flutter_keyboard_size 0.1.2+2 | |-- flutter... | '-- provider... |-- flutter_staggered_grid_view 0.3.3 | '-- flutter... |-- flutter_svg 0.19.1 | |-- flutter... | |-- meta... | |-- path_drawing 0.4.1+1 | | |-- flutter... | | |-- meta... | | |-- path_parsing 0.1.4 | | | |-- meta... | | | '-- vector_math... | | '-- vector_math... | |-- vector_math... | '-- xml 4.5.1 | |-- collection... | |-- convert... | |-- meta... | '-- petitparser 3.1.0 | '-- meta... |-- flutter_swipe_action_cell 1.2.2 | '-- flutter... |-- flutter_test 0.0.0 | |-- async... | |-- boolean_selector 2.1.0-nullsafety.1 | | |-- source_span... | | '-- string_scanner... | |-- characters... | |-- charcode 1.2.0-nullsafety.1 | |-- clock 1.1.0-nullsafety.1 | |-- collection... | |-- fake_async 1.2.0-nullsafety.1 | | |-- clock... | | '-- collection... | |-- flutter... | |-- matcher 0.12.10-nullsafety.1 | | '-- stack_trace... | |-- meta... | |-- path... | |-- source_span 1.8.0-nullsafety.2 | | |-- charcode... | | |-- collection... | | |-- path... | | '-- term_glyph... | |-- stack_trace... | |-- stream_channel 2.1.0-nullsafety.1 | | '-- async... | |-- string_scanner 1.1.0-nullsafety.1 | | |-- charcode... | | '-- source_span... | |-- term_glyph 1.2.0-nullsafety.1 | |-- test_api 0.2.19-nullsafety.2 | | |-- async... | | |-- boolean_selector... | | |-- collection... | | |-- matcher... | | |-- meta... | | |-- path... | | |-- source_span... | | |-- stack_trace... | | |-- stream_channel... | | |-- string_scanner... | | '-- term_glyph... | |-- typed_data... | '-- vector_math... |-- geoflutterfire 2.2.1 | |-- cloud_firestore... | |-- flutter... | '-- rxdart... |-- get 3.16.0 | '-- flutter... |-- google_maps_webservice 0.0.18 | |-- http... | '-- meta... |-- google_sign_in 4.5.5 | |-- flutter... | |-- google_sign_in_platform_interface 1.1.2 | | |-- flutter... | | |-- meta... | | '-- quiver... | |-- google_sign_in_web 0.9.2 | | |-- flutter... | | |-- flutter_web_plugins... | | |-- google_sign_in_platform_interface... | | |-- js... | | '-- meta... | '-- meta... |-- hive 1.4.4+1 | |-- crypto... | '-- meta... |-- hive_flutter 0.3.1 | |-- flutter... | |-- hive... | |-- path 1.8.0-nullsafety.1 | '-- path_provider 1.6.27 | |-- flutter... | |-- path_provider_linux 0.0.1+2 | | |-- flutter... | | |-- path... | | |-- path_provider_platform_interface... | | '-- xdg_directories 0.1.2 | | |-- meta... | | |-- path... | | '-- process 3.0.13 | | |-- file... | | |-- intl... | | |-- meta... | | |-- path... | | '-- platform... | |-- path_provider_macos 0.0.4+4 | | '-- flutter... | |-- path_provider_platform_interface 1.0.3 | | |-- flutter... | | |-- meta... | | |-- platform 2.2.1 | | '-- plugin_platform_interface... | '-- path_provider_windows 0.0.4+1 | |-- ffi 0.1.3 | |-- flutter... | |-- meta... | |-- path... | |-- path_provider_platform_interface... | '-- win32 1.7.3 | '-- ffi... |-- hive_generator 0.8.1 | |-- analyzer 0.40.4 | | |-- _fe_analyzer_shared 11.0.0 | | | '-- meta... | | |-- args... | | |-- charcode... | | |-- cli_util 0.2.0 | | | '-- path... | | |-- collection... | | |-- convert... | | |-- crypto... | | |-- glob... | | |-- meta... | | |-- package_config... | | |-- path... | | |-- pub_semver... | | |-- source_span... | | |-- watcher... | | '-- yaml... | |-- build... | |-- dartx 0.5.0 | | |-- characters... | | |-- collection... | | |-- crypto... | | |-- meta... | | |-- path... | | '-- time 1.3.0 | |-- hive... | '-- source_gen 0.9.7+1 | |-- analyzer... | |-- async... | |-- build... | |-- dart_style... | |-- glob... | |-- meta... | |-- path... | |-- pedantic... | '-- source_span... |-- http 0.12.2 | |-- http_parser 3.1.4 | | |-- charcode... | | |-- collection... | | |-- source_span... | | |-- string_scanner... | | '-- typed_data... | |-- path... | '-- pedantic 1.9.2 | '-- meta... |-- image 2.1.19 | |-- archive 2.0.13 | | |-- args... | | |-- crypto... | | '-- path... | |-- meta... | '-- xml... |-- json_annotation 3.1.0 |-- json_serializable 3.5.0 | |-- analyzer... | |-- build... | |-- build_config... | |-- json_annotation... | |-- meta... | |-- path... | '-- source_gen... |-- like_button 1.0.4 | '-- flutter... |-- liquid_swipe 1.5.0 | |-- flutter... | '-- provider 4.3.2+2 | |-- collection... | |-- flutter... | '-- nested 0.0.4 | '-- flutter... |-- modal_progress_hud 0.1.3 | '-- flutter... |-- package_info 0.4.3+2 | '-- flutter... |-- percent_indicator 2.1.9 | '-- flutter... |-- photo_view 0.10.3 | '-- flutter... |-- sign_in_with_apple 2.5.4 | |-- flutter... | '-- meta... |-- skeleton_text 1.0.0 | '-- flutter... |-- snapping_sheet 2.0.2 | '-- flutter... |-- sqflite 1.3.2+2 | |-- flutter... | |-- path... | '-- sqflite_common 1.0.3 | |-- meta... | |-- path... | '-- synchronized 2.2.0+2 |-- supercharged 1.11.1 | |-- flutter... | '-- supercharged_dart 1.3.1 |-- syncfusion_flutter_sliders 18.3.53-beta | |-- flutter... | |-- intl 0.16.1 | | '-- path... | '-- syncfusion_flutter_core 18.3.53 | |-- flutter... | '-- pedantic... |-- video_compress 2.1.1 | '-- flutter... '-- visibility_detector 0.1.5 '-- flutter... ```

benfgit commented 3 years ago

Have you checked this and #3671 ?

awaik commented 3 years ago

Yes, I've checked them.

Here https://github.com/FirebaseExtended/flutterfire/issues/3671 was the workaround for firestore. Here was suggested to initiate the plugin within isolate.

But it is impossible for [firebase_storage] plugin because it demands to use WidgetsFlutterBinding.ensureInitialized() before start.

And it is impossible to use WidgetsFlutterBinding.ensureInitialized() into isolate.

markusaksli-nc commented 3 years ago

Hi @awaik Could you please provide a minimal complete reproducible code sample and your flutter pub deps -- --style=compact (instead of the verbose tree). Thank you

benfgit commented 3 years ago

Can you try isolate_handler?

awaik commented 3 years ago

Hey @markusaksli-nc I made a repository https://github.com/awaik/test_firebase_storage_isolate It works on the iPhone simulator (already connected with firebase storage)

There are 2 cases:

  1. usual (works, uploading files)
  2. isolate - gives error +++++error inside isolate - ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized. 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. 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.

Results of flutter pub deps -- --style=compact (instead of the verbose tree)

Click To Expand ``` Dart SDK 2.10.5 Flutter SDK 1.22.6 teststorageisolate 1.0.0+1 dependencies: - cupertino_icons 1.0.0 - file_picker 2.1.5+1 [flutter flutter_web_plugins flutter_plugin_android_lifecycle plugin_platform_interface] - firebase_storage 4.0.1 [flutter firebase_core] - flutter 0.0.0 [characters collection meta typed_data vector_math sky_engine] dev dependencies: - flutter_test 0.0.0 [flutter test_api path fake_async clock stack_trace vector_math async boolean_selector characters charcode collection matcher meta source_span stream_channel string_scanner term_glyph typed_data] transitive dependencies: - async 2.5.0-nullsafety.1 [collection] - boolean_selector 2.1.0-nullsafety.1 [source_span string_scanner] - characters 1.1.0-nullsafety.3 - charcode 1.2.0-nullsafety.1 - clock 1.1.0-nullsafety.1 - collection 1.15.0-nullsafety.3 - fake_async 1.2.0-nullsafety.1 [clock collection] - firebase_core 0.5.3 [firebase_core_platform_interface flutter quiver meta firebase_core_web] - firebase_core_platform_interface 2.1.0 [flutter meta plugin_platform_interface quiver] - firebase_core_web 0.2.1+1 [firebase_core_platform_interface flutter flutter_web_plugins meta js] - flutter_plugin_android_lifecycle 1.0.11 [flutter] - flutter_web_plugins 0.0.0 [flutter characters collection meta typed_data vector_math] - js 0.6.2 - matcher 0.12.10-nullsafety.1 [stack_trace] - meta 1.3.0-nullsafety.3 - path 1.8.0-nullsafety.1 - plugin_platform_interface 1.0.3 [meta] - quiver 2.1.5 [matcher meta] - sky_engine 0.0.99 - source_span 1.8.0-nullsafety.2 [charcode collection path term_glyph] - stack_trace 1.10.0-nullsafety.1 [path] - stream_channel 2.1.0-nullsafety.1 [async] - string_scanner 1.1.0-nullsafety.1 [charcode source_span] - term_glyph 1.2.0-nullsafety.1 - test_api 0.2.19-nullsafety.2 [async boolean_selector collection meta path source_span stack_trace stream_channel string_scanner term_glyph matcher] - typed_data 1.3.0-nullsafety.3 [collection] - vector_math 2.1.0-nullsafety.3 ```
markusaksli-nc commented 3 years ago

That is quite an old version of firebase storage but I was able to reproduce this on the latest master 1.26.0-18.0.pre.90 with

  firebase_core: ^0.7.0
  firebase_storage: ^7.0.0
flutter doctor -v ```console [√] Flutter (Channel master, 1.26.0-18.0.pre.90, on Microsoft Windows [Version 10.0.19041.746], locale et-EE) • Flutter version 1.26.0-18.0.pre.90 at C:\Development\flutter_master • Framework revision 25134a16d1 (2 days ago), 2021-01-29 23:52:30 -0800 • Engine revision ce14c8a841 • Dart version 2.12.0 (build 2.12.0-276.0.dev) [√] Android toolchain - develop for Android devices (Android SDK version 30.0.2) • Android SDK at C:\Users\marku\AppData\Local\Android\sdk • Platform android-30, build-tools 30.0.2 • Java binary at: C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.7042882\jre\bin\java • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) • All Android licenses accepted. [√] Chrome - develop for the web • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe [√] Visual Studio - develop for Windows (Visual Studio Community 2019 16.7.7) • Visual Studio at C:\Program Files (x86)\Microsoft Visual Studio\2019\Community • Visual Studio Community 2019 version 16.7.30621.155 • Windows 10 SDK version 10.0.18362.0 [√] Android Studio (version 4.1.0) • Android Studio at C:\Users\marku\AppData\Local\JetBrains\Toolbox\apps\AndroidStudio\ch-0\201.7042882 • Flutter plugin can be installed from: https://plugins.jetbrains.com/plugin/9212-flutter • Dart plugin can be installed from: https://plugins.jetbrains.com/plugin/6351-dart • Java version OpenJDK Runtime Environment (build 1.8.0_242-release-1644-b01) [√] Connected device (4 available) • SM G950F (mobile) • ce12171c51cc001c03 • android-arm64 • Android 9 (API 28) • Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19041.746] • Chrome (web) • chrome • web-javascript • Google Chrome 88.0.4324.104 • Edge (web) • edge • web-javascript • Microsoft Edge 88.0.705.56 • No issues found! ```
Minimal reproducible code sample ```dart import 'dart:io'; import 'dart:isolate'; import 'package:file_picker/file_picker.dart'; import 'package:firebase_core/firebase_core.dart'; import 'package:flutter/material.dart'; import 'package:firebase_storage/firebase_storage.dart'; FirebaseApp app; Future main() async { WidgetsFlutterBinding.ensureInitialized(); app = await Firebase.initializeApp(); runApp(MyApp()); } class MyApp extends StatelessWidget { // This widget is the root of your application. @override Widget build(BuildContext context) { return MaterialApp( title: 'Flutter Demo', theme: ThemeData( primarySwatch: Colors.blue, visualDensity: VisualDensity.adaptivePlatformDensity, ), home: MyHomePage(), ); } } class MyHomePage extends StatefulWidget { @override _MyHomePageState createState() => _MyHomePageState(); } class _MyHomePageState extends State { String _text = 'Choose image to upload'; Isolate _isolate; ReceivePort _receivePort; SendPort sendPort; @override Widget build(BuildContext context) { return Scaffold( appBar: AppBar( title: Text('test isolate storage'), ), body: Center( child: Column( mainAxisAlignment: MainAxisAlignment.center, children: [ SelectableText( _text, ), Text( '', style: Theme.of(context).textTheme.headline4, ), ], ), ), floatingActionButton: FloatingActionButton( onPressed: _incrementCounter, tooltip: 'Choose file to upload', child: Icon(Icons.file_upload), ), // This trailing comma makes auto-formatting nicer for build methods. ); } void _incrementCounter() async { FilePickerResult result = await FilePicker.platform.pickFiles( allowMultiple: false, type: FileType.image, ); /// FIRST CASE - direct upload within main isolate /// it works only with /// WidgetsFlutterBinding.ensureInitialized(); /// in the start of the app Reference ref = FirebaseStorage.instance.ref().child('tests/' + UniqueKey().toString()); UploadTask uploadTask = ref.putFile(File(result.files.last.path)); TaskSnapshot storageTaskSnapshot = await uploadTask.whenComplete(() => print("normal completed")); _text = await storageTaskSnapshot.ref.getDownloadURL(); ///SECOND CASE - we want to upload with isolate _isolate = await Isolate.spawn(upload, File(result.files.last.path)); setState(() {}); } static void upload(data) async { await Firebase.initializeApp(); Reference ref = FirebaseStorage.instance.ref().child('tests/' + UniqueKey().toString()); UploadTask uploadTask = ref.putFile(File(data.files.last.path)); uploadTask.whenComplete(() => print("isolate completed")); } } ```
Logs ```console E/flutter (29958): [ERROR:flutter/runtime/dart_isolate.cc(1137)] Unhandled exception: E/flutter (29958): Null check operator used on a null value E/flutter (29958): #0 MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:142:86) E/flutter (29958): #1 MethodChannel._invokeMethod (package:flutter/src/services/platform_channel.dart:148:36) E/flutter (29958): #2 MethodChannel.invokeMethod (package:flutter/src/services/platform_channel.dart:331:12) E/flutter (29958): #3 MethodChannel.invokeListMethod (package:flutter/src/services/platform_channel.dart:344:41) E/flutter (29958): #4 MethodChannelFirebase._initializeCore (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:30:36) E/flutter (29958): #5 MethodChannelFirebase.initializeApp (package:firebase_core_platform_interface/src/method_channel/method_channel_firebase.dart:75:13) E/flutter (29958): #6 Firebase.initializeApp (package:firebase_core/src/firebase.dart:44:25) E/flutter (29958): #7 _MyHomePageState.upload (package:triage/main.dart:93:20) E/flutter (29958): #8 _delayEntrypointInvocation. (dart:isolate-patch/isolate_patch.dart:286:17) E/flutter (29958): #9 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:184:12) ```

The issue is caused by the fact that we cannot initialize Firebase in a non-main isolate due to the reliance on platform channel methods.

There have been a few issues about this before https://github.com/FirebaseExtended/flutterfire/issues/3124

@awaik For now you will just have to find a different solution.

AntoineChauviere commented 1 year ago

Hello, has a solution to this problem been found?

russellwheatley commented 10 months ago

Hey @awaik, it is possible to use isolates for FlutterFire but it depends on whether the underlying API communicates witht he native platform via method channel or event channel. If the API uses an event channel, it won't work. If the API uses a platform channel, it will work.

Here is an open issue to allow communication from native -> dart which would allow event channel communication: https://github.com/flutter/flutter/issues/119207

This article illustrates how to use isolates for a Firestore API that is using the method channel under the hood: https://invertase.io/blog/improve-flutter-performance-with-background-isolates-in-flutter-3-7

I'm going to close this issue out in favour of this issue which is the same problem: https://github.com/firebase/flutterfire/issues/10514