Luciano Paiva
Luciano Paiva

Reputation: 13

Flutter - function to create user using firebase

I'm learning flutter on my own, and I'm using firebase on a small project. I'm in the stage of working with login and registrations, so I needed to use a plugin called firebase_auth: ^ 0.20.0 + 1. However, I have reached a point in my code where one occurs, and I don't know what can cause it. When I create the user, I indicate an email and a password in .createUserWithEmailAndPassword, if it works it should return a function to indicate success in the creation, there is no error log because I did not complete the code and performed this step, however the error is in the syntax that I'm using in this function, it doesn't seem to be in accordance with the syntax of firebase, I must be doing something wrong. He says these lines are wrong .then ((user) {firebaseUser = user;

import 'package:firebase_auth/firebase_auth.dart';
import 'package:scoped_model/scoped_model.dart';
import 'dart:async';
import 'package:flutter/material.dart';

class UserModel extends Model {
  //usuario atual

  FirebaseAuth _auth = FirebaseAuth.instance;
  FirebaseUser firebaseUser;
  Map<String, dynamic> userData = Map();

  bool isLoading = false;
  void signUp(Map<String, dynamic> userData, String pass, VoidCallback onSucess,
      VoidCallback onFail) {
    isLoading = true;
    notifyListeners();
    _auth
        .createUserWithEmailAndPassword(
            email: userData["email"], password: pass)
        .then((user) async{
      firebaseUser = user;

      onSucess();
      isLoading = false;
      notifyListeners();
    }).catchError((e) {
      onFail();
      isLoading = false;
      notifyListeners();
    });
  }

  void signIn() async {
    isLoading = true;
    notifyListeners();
    await Future.delayed(Duration(seconds: 3));

    isLoading = false;
    notifyListeners();
  }

  void recoverPass() {}

  //bool isLoggedIn() {}
}

class FirebaseUser {}

Upvotes: 1

Views: 619

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598728

The call to createUserWithEmailAndPassword creates a user and returns a Future<UserCredential>.

In the then (which takes care of the Future part of this result), you assign the UserCredential to your firebaseUser variable, which is defined as FirebaseUser firebaseUser. And the error message tell you that FirebaseUser and UserCredential are not compatible types.

To get the FirebaseUser from the UserCredential, use:

.then((credentials) async{
    firebaseUser = credentials.user;

Depending on the version of the firebase_auth plugin you use, you might need to declare firebaseUser as:

User firebaseUser

That is the correct type in the latest version of the FlutterFire libraries, while older builds had it as FirebaseUser.

I've linked the relevant reference documentation above, as I find that most helpful when troubleshooting these types of problems. I highly recommend keeping them open while you're learning about the API.

Upvotes: 1

Related Questions