Skip to content

Unhandled Exception: ServicesBinding.defaultBinaryMessenger was accessed before the binding was initialized #45

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
draylegend opened this issue Feb 11, 2020 · 1 comment

Comments

@draylegend
Copy link

draylegend commented Feb 11, 2020

Hi @escamoteur and thank you for your talk last week in Köln :) so I decided to give get_it a try but unfortunately got into troubles.

How can I use AuthService inside of ExpertService?

// main.dart

import 'package:my-app/features/utils.dart';

void main() {
  registerServices();
  runApp(App());
}
// utils.dart

import 'package:get_it/get_it.dart';

final GetIt getIt = GetIt.I;

void registerServices() {
  getIt.registerSingleton(AuthService());
  getIt.registerSingleton(NavService());
  getIt.registerSingleton(ExpertService());
}
// auth.service.dart

import 'package:firebase_auth/firebase_auth.dart';

class AuthService {
  FirebaseAuth _auth = FirebaseAuth.instance;

  Stream<FirebaseUser> get firebaseUser$ => _auth.onAuthStateChanged;

  void signIn(String email, String password) async {
    try {
      await _auth.signInWithEmailAndPassword(email: email, password: password);
    } catch (e) {
      print('!!! ${e.message} !!!');
    }
  }
}
// expert.service.dart

import 'package:rxdart/rxdart.dart';
import 'package:my-app/core/services/database/database.service.dart';
import 'package:my-app/features/auth/auth.service.dart';
import 'package:my-app/features/expert/expert.model.dart';
import 'package:my-app/features/utils.dart';

// DatabaseService handles document reads and writes
class ExpertService extends DatabaseService<Expert> {
  Stream<Expert> get model$ => FirebaseAuth.instance.onAuthStateChanged
      .switchMap((user) => user == null ? Stream.empty() : getData$(user.uid));

  ExpertService() : super('experts');
}

The error :(

[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 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.
#0      defaultBinaryMessenger.<anonymous closure> (package:flutter/src/services/binary_messenger.dart:76:7)
#1      defaultBinaryMessenger (package:flutter/src/services/binary_messenger.dart:89:4)
#2      MethodChannel.binaryMessenger (package:flutter/src/services/platform_channel.dart:140:62)
#3      MethodChannel.setMethodCallHandler (package:flutter/src/services/platform_channel.dart:375:5)
#4      new MethodChannelFirestore (package:cloud_firestore_platform_interface/src/method_channel/method_channel_firestore.dart:27:13)
#5      FirestorePlatform.instance (package:cloud_firestore_platform_interface/cloud_firestore_platform_interface.dart:62:19)
#6      new Firestore (package:cloud_firestore/src/firestore.dart:26:42)
#7      Firestore.instance (package:cloud_firestore/src/firestore.dart:29:36)
#8      new DatabaseService (package:my-app/core/services/database/database.service.dart:8:41)
#9      new ExpertService (package:my-app/features/expert/expert.service.dart)
#10     registerServices (package:my-app/features/utils.dart:13:27)
#11     main (package:my-app/main.dart:11:3)
#12     _runMainZoned.<anonymous closure>.<anonymous closure> (dart:ui/hooks.dart:239:25)
#13     _rootRun (dart:async/zone.dart:1126:13)
#14     _CustomZone.run (dart:async/zone.dart:1023:19)
#15     _runZoned (dart:async/zone.dart:1518:10)
#16     runZoned (dart:async/zone.dart:1502:12)
#17     _runMainZoned.<anonymous closure> (dart:ui/hooks.dart:231:5)
#18     _startIsolate.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:307:19)
#19     _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:174:12)

Thank you in advance and I hope you can help me!

@escamoteur
Copy link
Collaborator

I think it's exactly stated in your error message:

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants