Flutter-使用Firebase创建用户的功能 [英] Flutter - function to create user using firebase

查看:52
本文介绍了Flutter-使用Firebase创建用户的功能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我自己学习波动,并且在一个小项目上使用Firebase.我正处于登录和注册的阶段,因此我需要使用一个名为 firebase_auth:^ 0.20.0 + 1 的插件.但是,我已经在代码中达到了发生某点的地步,并且我不知道是什么原因引起的.创建用户时,我会在 .createUserWithEmailAndPassword 中指定一封电子邮件和密码,如果可以正常工作,它应该返回一个函数来指示创建成功,因为没有完成创建操作,所以没有错误日志代码并执行了此步骤,但是错误是我在此函数中使用的语法错误,它似乎与firebase的语法不一致,我必须做错了什么.他说这些行是错误的.然后((user){firebaseUser = user;

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 {}

推荐答案

then (将处理此结果的 Future 部分)中,您分配

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.

要从 UserCredential 中获取 FirebaseUser ,请使用:

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

根据您使用的 firebase_auth 插件的版本,您可能需要将 firebaseUser 声明为:

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

User firebaseUser

这是最新版本的FlutterFire库中的正确类型,而较早的版本将其命名为 FirebaseUser .

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

我已经链接了上面的相关参考文档,因为我发现在对这些类型的问题进行故障排除时最有帮助.我强烈建议您在学习API时将其保持打开状态.

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.

这篇关于Flutter-使用Firebase创建用户的功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆