Valdo Amaral
Valdo Amaral

Reputation: 11

I'm getting a error when I try to Sign up user

I'm getting this error when I try to sign up user. error: A value of type 'AuthResult' can't be assigned to a variable of type 'FirebaseUser'. (invalid_assignment at [projectfooddelivery] lib\Src\models\user.dart:28). When I try to create the user in the firebase I cant do anything. I want to know how can I fix this error and how can I test if my app is properly connected to the firebase.

Code:

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);
 }
}

Upvotes: 1

Views: 61

Answers (1)

Peter Haddad
Peter Haddad

Reputation: 80914

The error is here:

   _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

Upvotes: 1

Related Questions