尝试注册用户时出现此错误。 错误:无法将类型“ 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);
}
}
答案 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
的当前用户