Flutter-异步函数返回null

时间:2019-03-10 00:25:35

标签: firebase firebase-realtime-database dart flutter

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

以下是两个相关的代码段:

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

2 个答案:

答案 0 :(得分:2)

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

答案 1 :(得分:1)

问题在于,链接.then的类型“覆盖”先前的诺言的返回类型。

此函数自行返回Future<FirebaseUser>

_firebaseAuth.createUserWithEmailAndPassword(...);

但是,您有一个.then链什么也没有返回,这就是为什么没有为Future的最终结果分配任何值,并且该值仍然为null的原因:

.then((newUser) {
  var obj = {
    "active": true,
    "public": true,
    "email": email,
    "username":username
  };
  _profileRef.child(newUser.uid).set(obj).then((_) {
    print("inside");
  };
  // Need to return newUser here.
};

您可以添加return newUser;

_profileRef.child(newUser.uid).set(obj).then((_) {
    print("inside");
};
return newUser;

或遵循Richard的答案,该答案完全摆脱了.then,而仅使用await,这使您的代码看起来更简洁,更易于阅读,尤其是在异步链接的情况下。