【问题标题】:flutter & firebase : multiple error with the auth颤振和火力:身份验证的多个错误
【发布时间】:2021-08-27 18:14:29
【问题描述】:

我正在为用户创建/日志,但我有多个错误,这是我的代码,在我的错误之后: 登录控制器.dart:

  String? _mail;
  String? _password;
  String? _prenom;
  String? _nom;

  _handlelog(){
    if(_mail != null){
      if(_password != null){
        if(_log == true){
          //se connecter
          FirebaseHelper().handleSignIn(_mail, _password).then(((User user) {
            print("Nous avons un user");
          })).catchError((error){
            alerte(error.toString());
          });
        }else{
          if(_prenom != null){
            if(_nom != null){
              //créer un compte avec les données de l'utilisateur
              FirebaseHelper().handleCreate(_mail, _password, _prenom, _nom).then(((User user) {
                print("Nous avons pu créer un user");
              })).catchError((error){
                alerte(error.toString());
              });
            }else{
              //Alert nom
              alerte("veuillez entrer un nom pour continuer");
            }
          }else{
            //Alert prenom
            alerte("veuillez entrer un prenom pour continuer");
          }
        }
      }else{
        //Alert password
        alerte("le mot de passe est vide");
      }
    }else{
      //Alert mail
      alerte("l'adresse mail est vide");
    }
  }

FirebaseHelper.dart:

class FirebaseHelper {

  //Authentification
  final auth = FirebaseAuth.instance;

  Future<UserCredential> handleSignIn(String mail, String password) async{
    final UserCredential user = await auth.signInWithEmailAndPassword(email: mail, password: password);
    return user;
  }

  Future<UserCredential> handleCreate(String mail, String password, String prenom, String nom) async{
    final UserCredential credential = await FirebaseAuth.instance.createUserWithEmailAndPassword(email: mail, password: password);
    String uid = credential.user!.uid;
    Map<String, String> map = {
      "uid" : uid,
      "prenom" : prenom,
      "nom" : nom,
    };
    addUser(uid, map);
    return credential;
  }

  //database

  static final base = FirebaseDatabase.instance.reference();
  // ignore: non_constant_identifier_names
  final base_user = base.child("users");

  addUser(String uid, Map map){
    base_user.child(uid).set(map);
  }

}

在 LoginController.dart 我有 The argument type 'String?' can't be assigned to the parameter type 'String'. The argument type 'Null Function(User)' can't be assigned to the parameter type 'FutureOr&lt;dynamic&gt; Function(UserCredential)'. 我不知道我能做什么,我在文档上搜索了很长时间,在这个网站上找到了更多细节,但我什么也没找到,我很绝望

【问题讨论】:

    标签: firebase flutter dart firebase-authentication


    【解决方案1】:

    我用过这个,也许会有所帮助

    class AuthServices {
    final FirebaseAuth _auth = FirebaseAuth.instance;
      //Create user object based on firebase user
      UserData _userFromFirebaseUser(User user) {
        return user != null ? UserData(uid: user.uid, email: user.email) : null;
      }
    
      //Auth change user stream
      Stream<UserData> get uservalue {
        return _auth.authStateChanges().map((User uservalue) => _userFromFirebaseUser(uservalue));
      }
    
    
      // Sign In with email & password
      Future sigInUser(String email, String password) async {
        try {
          UserCredential result = await _auth.signInWithEmailAndPassword(email: email, password: password);
          User userval = result.user;
    
          return _userFromFirebaseUser(userval);
    
        }
    
        catch(e) {
          print(e.toString());
          return null;
        }
      }
    
      // Register with email & password
      Future registerUser(String email, String password) async {
        try {
          UserCredential result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
          User userval = result.user;
    
          return _userFromFirebaseUser(userval);
    
        }
        catch(e) {
          print(e.toString());
        }
    
      }
    }
    

    【讨论】:

    • 是用于 LoginController 还是 FirebaseHelper?
    • 用于 Firebasehelper
    【解决方案2】:

    错误 1:

    参数类型“字符串?”不能分配给参数类型 '字符串'。

    显示上面的错误是因为_mail_password_prenom_nom 的类型为String?,这意味着它们可以为空,而FirebaseHelper().handleCreate() 方法的参数类型为@987654329 @ 与String? 不同。

    后缀感叹号 (!) 表示左边的表达式 将其转换为其底层的不可为空的类型

    https://dart.dev/null-safety/understanding-null-safety#null-assertion-operator

    所以添加! 会将String? 对象转换为类型String

    错误 2:

    参数类型“Null Function(User)”不能分配给 参数类型'FutureOr Function(UserCredential)'。

    上述错误的原因是FirebaseHelper().handleCreate() 返回了Future&lt;UserCredential&gt; 的类型,但您在LoginController.dart 文件中使用User 作为类型。

    解决方案:

    您需要更新这部分代码:

    FirebaseHelper().handleCreate(_mail, _password, _prenom, _nom).then(((User user) {
    

    到这里:

    FirebaseHelper().handleCreate(_mail!, _password!, _prenom!, _nom!).then(((UserCredential user) {
    

    【讨论】:

    • 我有一个错误,当我想创建或登录一个帐户(在我的应用程序上)时,它是标记:[firebase_auth/internal-error]{"error":{"code":400,"message":"CONFIGURATION_NOT_FOUND","errors":[{"message":"CONFIGURATION_NOT_FOUND","domain":"global","reason":"invalid"}]}} 如果你想要我的所有代码,所以我可以发布一个链接:link
    • 您是否在 Firebase 上注册了您的应用/您正在使用的身份验证服务?查看此链接:stackoverflow.com/a/65453612/11039164.
    • 另外,我的回答能解决你最初的问题吗?
    猜你喜欢
    • 2021-08-26
    • 2020-09-07
    • 1970-01-01
    • 2021-06-05
    • 2023-03-22
    • 1970-01-01
    • 2019-01-09
    • 2019-05-31
    • 2021-03-22
    相关资源
    最近更新 更多