Home > Software design >  PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null
PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null

Time:11-29

I am new with flutter and am working through this code lab. https://firebase.google.com/codelabs/firebase-get-to-know-flutter I am on part 4 of the code lab (setting up RSVP) and encountered this error when I tried running the app. PlatformException (PlatformException(channel-error, Unable to establish connection on channel., null, null))

Here is what the code looks like:

class ApplicationState extends ChangeNotifier {
  ApplicationState() {
    init();
  }

  bool _loggedIn = false;
  bool get loggedIn => _loggedIn;

  Future<void> init() async {
    await Firebase.initializeApp(
        options: DefaultFirebaseOptions.currentPlatform);

    FirebaseUIAuth.configureProviders([
      EmailAuthProvider(),
    ]);

    FirebaseAuth.instance.userChanges().listen((user) {
      if (user != null) {
        _loggedIn = true;
      } else {
        _loggedIn = false;
      }
      notifyListeners();
    });
  }
}

The error occurs on await Firebase.initializeApp. I've ran flutter pub upgrade, flutter clean, then flutter pub get but the problem still persists. What should I do?

CodePudding user response:

You should write

await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);

in main.dart instead of here.

Try the following in main.dart:

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(options: DefaultFirebaseOptions.currentPlatform);
  …
}
  • Related