【问题标题】:Flutter Web: MissingPluginException(No implementation found for method startListeningAuthState on channel plugins.flutter.io/firebase_auth)Flutter Web:MissingPluginException(在通道 plugins.flutter.io/firebase_auth 上找不到方法 startListeningAuthState 的实现)
【发布时间】:2020-09-29 18:35:53
【问题描述】:

我最近将我的 Flutter Firebase 应用转换为 Flutter Web,尝试登录或注册后出现此错误:

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

我在我的 Firebase 项目中添加了新的网络应用,粘贴了代码,但它似乎不起作用。有什么解决办法吗?我想尽可能地保持我的代码相同,但我会感谢每一个答案。谢谢! 我的 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>

我的 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

我的 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;
    }
  }
}

【问题讨论】:

    标签: firebase flutter native progressive-web-apps


    【解决方案1】:

    将您的 FireAuth 版本更新为 firebase_auth:^0.18.4+1 如果在更新 pubspec 期间出现任何其他错误,请将其他依赖项更新为更新版本。

    [回复 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。

    【讨论】:

    • 嘿伙计!非常感谢您的回答。我做了同样的事情并且它有效,但我忘了发布答案。如果这对某人不起作用,请尝试将 firebase_auth_web 添加到您的项目中
    • @Henzelix 对不起,我做了这两件事,但仍然有上述问题,我还需要做些什么吗?
    • @nike 如果您的项目中有其他 Firebase 依赖项,请尝试添加此依赖项的 Web 版本。例如,如果您使用 firebase-storage,请尝试同时添加 firebase-storage-web
    【解决方案2】:

    对我有用的是删除/注释掉所有与firebase相关的依赖项并使用flutter pub add *dependency name*一一重新安装

    这确保我安装了最新的依赖项并且没有冲突

    【讨论】:

      猜你喜欢
      • 2019-09-06
      • 2020-08-10
      • 2020-08-18
      • 1970-01-01
      • 2021-03-30
      • 2021-02-01
      • 1970-01-01
      • 2021-10-06
      • 2020-04-14
      相关资源
      最近更新 更多