尝试注册用户时出现错误

时间:2020-05-17 09:33:17

标签: android firebase flutter dart

尝试注册用户时出现此错误。 错误:无法将类型“ AuthResult”的值分配给类型“ FirebaseUser”的变量。 (位于[projectfooddelivery] lib \ Src \ models \ user.dart:28处的invalid_assignment)。当我尝试在Firebase中创建用户时,我无能为力。我想知道如何解决此错误,以及如何测试我的应用是否正确连接到Firebase。

代码:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:scoped_model/scoped_model.dart';

class UserModel extends Model{
    FirebaseAuth _auth=FirebaseAuth.instance;
    FirebaseUser firebaseUser;
    Map<String,dynamic> userData=Map();
    bool isLoading=false;



  void signIn()async{
 isLoading=true;
 notifyListeners();
 await Future.delayed(Duration(seconds: 3));
 isLoading=false;
 notifyListeners();
  }
 void signUp({@required Map<String,dynamic>userData,@required String pass,@required VoidCallback onSuccess,@required VoidCallback onFail} ){
   isLoading=true;
   notifyListeners();
   _auth.createUserWithEmailAndPassword(
       email: userData["email"],
       password: pass
   ).then((user)async{
     firebaseUser=user;
     await _saveUserData(userData);
     onSuccess();
     isLoading=false;
     notifyListeners();
   }).catchError((e){
     onFail();
     isLoading=false;
     notifyListeners();
   });

 }
 void recoverPass(){

 }
 Future<Null> _saveUserData(Map<String,dynamic>userData)async{
   this.userData=userData;
   await Firestore.instance.collection("users").document(firebaseUser.uid).setData(userData);
 }
}

1 个答案:

答案 0 :(得分:1)

错误在这里:

   _auth.createUserWithEmailAndPassword(
       email: userData["email"],
       password: pass
   ).then((user)async{
     firebaseUser=user;

方法createUserWithEmailAndPassword返回Future<AuthResult>,因此user的类型为AuthResult,而firebaseUser的类型为FirebaseUser。您需要执行以下操作:

   _auth.createUserWithEmailAndPassword(
       email: userData["email"],
       password: pass
   ).then((user)async{
     firebaseUser = user.user;
     print(firebaseUser.uid);

AuthResult包含属性user,该属性返回类型为FirebaseUser的当前用户