Search code examples
firebaseflutternativeprogressive-web-apps

Flutter Web: MissingPluginException(No implementation found for method startListeningAuthState on channel plugins.flutter.io/firebase_auth)


I recently converted my Flutter Firebase app to to Flutter Web and I'm getting this error after trying to sign in or register:

MissingPluginException(No implementation found for method startListeningAuthState on channel plugins.flutter.io/firebase_auth)

I added new web app to my Firebase project, pasted the code and it doesn't seem to work. Any solutions? I would like to keep my code as much the same as it can, but i will apreciate every answer. Thanks! My index.html:

    <body>

      <!-- The core Firebase JS SDK is always required and must be listed first -->
      <script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-app.js"></script>

      <!-- TODO: Add SDKs for Firebase products that you want to use
           https://firebase.google.com/docs/web/setup#available-libraries -->
      <script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-analytics.js"></script>
      <script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-auth.js"></script>
      <script src="https://www.gstatic.com/firebasejs/7.21.1/firebase-firestore.js"></script>

      <script>
        // Your web app's Firebase configuration
        // For Firebase JS SDK v7.20.0 and later, measurementId is optional
        var firebaseConfig = {
          apiKey: "**",
          authDomain: "***",
          databaseURL: "h****",
          projectId: "%%%",
          storageBucket: "%%%%",
          messagingSenderId: "%%%%",
          appId: "%%%%",
          measurementId: "%%%%"
        };
        // Initialize Firebase
        if(!firebase.apps.length){
          firebase.initializeApp(firebaseConfig);
          firebase.analytics();
          firebase.auth();
          firebase.firestore();
        };
      </script>

      <!-- This script installs service_worker.js to provide PWA functionality to
           application. For more information, see:
           https://developers.google.com/web/fundamentals/primers/service-workers -->
      <script>
        if ('serviceWorker' in navigator) {
          window.addEventListener('load', function () {
            navigator.serviceWorker.register('flutter_service_worker.js');
          });
        }
      </script>
      <script src="main.dart.js" type="application/javascript"></script>
    </body>

My pubspec.yaml:

dependencies:
      flutter:
        sdk: flutter
      firebase_auth: ^0.14.0+5
      cloud_firestore: ^0.12.9+4
      firebase_core: ^0.4.4+3
      firebase_storage: 3.0.4
      provider: ^3.1.0
      image_cropper: ^1.2.0
      image_picker: ^0.6.5
      outline_gradient_button: ^1.0.0+2
      page_transition: ^1.1.6
      share: ^0.6.4+3
      intl: ^0.16.1
      photo_view: ^0.10.2
      skeleton_text: ^1.0.0
      url_launcher: ^5.4.5
      admob_flutter: ^0.3.4
      file_picker: ^2.0.0
      flutter_downloader: ^1.5.0
      path_provider: ^1.6.18
      permission_handler: ^5.0.1+1
      ext_storage: ^1.0.3
      connectivity: ^0.4.9+3

My auth.dart:

import 'package:classy/pages/blank.dart';
import 'package:flutter/material.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/services.dart';
import 'user.dart';

class AuthService {
  String exceptionError;

  final FirebaseAuth _auth = FirebaseAuth.instance;

  //Creating user object
  User userFromFirebaseUser(FirebaseUser user){
    return user != null ? User(uid: user.uid) : null;
  }

  void currentUser() async{
    final FirebaseUser user = await _auth.currentUser();
  }

  //auth change user stream
  Stream<User> get user{
    return _auth.onAuthStateChanged.map((FirebaseUser user) => userFromFirebaseUser(user));
  }

  //Registering with email and password
  Future register(String email, String password, context) async {
    try{
      AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      FirebaseUser user = result.user;
      Navigator.popUntil(context, ModalRoute.withName("/"));
//      Navigator.pushReplacementNamed(context, '/wrapper');
      return userFromFirebaseUser(user);

    }
    on PlatformException catch(e){
      if(e.message.startsWith('The email address is already in use')){
        exceptionError='Użytkownik z podanym adresem email już istnieje';
      }
      if(e.message.startsWith('The email address is badly formatted')){
        exceptionError='Wpisz poprawny adres email';
      }
      return null;
    }
  }

  //Logging with email and password
  Future login(String email, String password, context) async {
    try{
      AuthResult result = await _auth.signInWithEmailAndPassword(email: email, password: password);
      FirebaseUser user = result.user;
      Navigator.pop(context);
      return userFromFirebaseUser(user);
    }
    on PlatformException catch(e){
      if(e.message.startsWith('The password is invalid')){
        exceptionError='Wpisz poprawne hasło';
      }
      if(e.message.startsWith('There is no user record')){
        exceptionError='Użytkownik z podanym email nie istnieje';
      }
      return null;
    }
  }
}


Solution

  • Update your FireAuth version to firebase_auth: ^0.18.4+1 if during the update of pubspec any other error was presented, update the other dependencies to updated versions.

    [Resposta em BR]

    atualize a versão do seu FireAuth para firebase_auth: ^0.18.4+1 se durante a atualização do pubspec algum outro erro foi apresentado atualize as demais dependências para versões atualizadas.