尝试注册用户时出现错误

尝试注册用户时出现此错误。 错误:无法将类型“ 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);
 }
}
评论
心的束缚
心的束缚

错误在这里:

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

The method createUserWithEmailAndPassword returns Future<AuthResult> therefore user is of type AuthResult, and firebaseUser is of type FirebaseUser. You need to do the following:

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

AuthResult contains the property user which returns the current user of type FirebaseUser

点赞
评论