首页 > 解决方案 > 无法从异步函数访问函数参数

问题描述

在注册的那一刻,当我尝试打印作为函数参数的用户名时,它不会在屏幕上打印任何内容,我不明白为什么。

 Future registerWithEmailAndPassword(String username, String email, String password) async {
    try {
      AuthResult result = await _auth.createUserWithEmailAndPassword(email: email, password: password);
      FirebaseUser user = result.user;
      // create a new document for the user with the uid
      print(username);
      await DatabaseService(uid: user.uid).updateUserData(username);
      return _userFromFirebaseUser(user);
    } catch (error) {
      print(error.toString());
      return null;
    } 
  }

编辑:这是调用 registerWithEmailAndPassword 函数的函数:

onPressed: () async {
                        if (error.length > 0)
                          error = "";
                        if (_formKey.currentState.validate()) {
                          setState(() => loading = true);
                          dynamic result = await _auth.registerWithEmailAndPassword(username, email, password);
                          if (result == null){
                            if (this.mounted){
                            setState(() {
                              error = 'Please supply a valid email';
                              loading = false;

                            });}
                          }

                        }
                      }

标签: flutterdart

解决方案


推荐阅读