• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    公众号

firebase - Flutter-异步函数返回null

[复制链接]
菜鸟教程小白 发表于 2022-9-1 00:11:19 | 显示全部楼层 |阅读模式 打印 上一主题 下一主题

我已经不停地工作了几天,即使经过搜索和挖掘,也没有弄清楚。

这是两个相关的代码段:

Future<FirebaseUser> signUp(String email, String password, String username) async {
FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(
    email: email, password: password).then((newUser) {
      var obj = {
        "active": true,
        "public": true,
        "email": email,
        "username":username
      };
      _profileRef.child(newUser.uid).set(obj).then((_) {
        print("inside");
        //print("new userId: ${newUser}");
        //return newUser;
      });
});
//print("outside");
return user;
}

和:
Future<void> register() async {
final formState = _formKey.currentState;

if(formState.validate()) {
  formState.save();

  try {
    //print("email: " + _email + ", pwd: " + _password);
    //FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email,password: _password);
    //String uid = await widget.auth.signIn(_email, _password);
    FirebaseUser user = await widget.auth.signUp(_email, _password, _username);

    print("uid: " + user.uid);
    Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomePage(auth: widget.auth, userId: user.uid, onSignedOut: widget.onSignedIn,)));
    //Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) => HomePage(auth: widget.auth, userId: uid, onSignedOut: widget.onSignedIn,)));
  } catch(e) {
    print(e);
  }
}
}

signUp()函数可正常运行,并在Firebase中正确创建用户,并在Firebase实时数据库中正确创建userProfile条目。但是,无论出于何种原因,我都无法在register()函数中重新获得实际的FirebaseUser对象。它总是返回如下错误:
    Connected Path: satisfied (Path is satisfied), interface: en0
Duration: 1.315s, DNS @0.001s took 0.004s, TCP @0.007s took 0.053s, TLS took 0.158s
bytes in/out: 5745/975, packets in/out: 9/9, rtt: 0.051s, retransmitted packets: 0, out-of-order packets: 0
[C3.1 8282B933-6D0B-4103-937C-173268FD0304 192.168.1.7:54700<->172.217.14.106:443]
Connected Path: satisfied (Path is satisfied), interface: en0
Duration: 0.441s, DNS @0.000s took 0.003s, TCP @0.005s took 0.054s, TLS took 0.152s
bytes in/out: 5040/1812, packets in/out: 9/9, rtt: 0.052s, retransmitted packets: 0, out-of-order packets: 0
flutter: NoSuchMethodError: The getter 'uid' was called on null.
Receiver: null
Tried calling: uid
flutter: inside



Best Answer-推荐答案


awaitthen结合使用通常会造成混淆。重构您的signUp方法以删除then

Future<FirebaseUser> signUp(String email, String password, String username) async {
  FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(
    email: email, password: password);
  var obj = {
        "active": true,
        "public": true,
        "email": email,
        "username": username,
      };
  await _profileRef.child(user.uid).set(obj);
  return user;
}

关于firebase - Flutter-异步函数返回null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55083338/

回复

使用道具 举报

懒得打字嘛,点击右侧快捷回复 【右侧内容,后台自定义】
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

关注0

粉丝2

帖子830918

发布主题
阅读排行 更多
广告位

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap