创建用户 – 此表达式的类型为 ‘void’,因此无法使用其值。

huangapple go评论74阅读模式
英文:

Flutter/Dart/Firebase Creating User - This expression has a type of 'void' so its value can't be used

问题

我有以下的代码,不知何故无法运行:

Future<void> createUserWithEmailAndPassword() async {
  //尝试创建用户
  try {
    UserCredential userCredential =
    await Auth().createUserWithEmailAndPassword(
      email: _controllerEmail.text,
      password: _controllerPassword.text
    );
  } 
  
  catch (e) {
    if (e is FirebaseAuthException) {
      setState(() {
        errorMessage = e.message;
      });
    }
  }
}

我想要添加userCredential变量,以便稍后可以使用它,但不知何故它给了我以下错误:

此表达式的类型为'void',因此无法使用其值。
尝试检查是否使用了正确的API;可能有一个返回void的函数或调用,你没有预期。还要检查可能也是void的类型参数和变量。dartuse_of_void_result

我想要使用userCredential变量,以便可以在Cloud Firestore中为用户的文档创建附加信息。

有关如何解决的任何想法吗?

英文:

I have following code which does not work somehow:

Future&lt;void&gt; createUserWithEmailAndPassword() async {
  //try nutzer erstellen
  try {
    UserCredential userCredential =
    await Auth().createUserWithEmailAndPassword(
      email: _controllerEmail.text,
      password: _controllerPassword.text
    );
  } 
  
  catch (e) {
    if (e is FirebaseAuthException) {
      setState(() {
        errorMessage = e.message;
      });
    }
  }
}

I want to add the userCredential variable so I can use it later on but somehow it give me the following error:

> This expression has a type of 'void' so its value can't be used.
Try checking to see if you're using the correct API; there might be a function or call that returns void you didn't expect. Also check type parameters and variables which might also be void.dartuse_of_void_result

I want to use the userCredential variable so I can create in Cloud Firestore additional information for a user in a document.

Any ideas on how to solve?

答案1

得分: 2

函数createUserWithEmailAndPassword确实返回一个包含UserCredential的Future。

请确保Auth类中使用的函数的返回类型与FirebaseAuth提供的createUserWithEmailAndPassword函数的返回类型匹配。

应该是这样的:

class Auth {
  static Future<UserCredential> createUserWithEmailAndPassword({
    required String email,
    required String password,
  }) =>
      FirebaseAuth.instance
          .createUserWithEmailAndPassword(email: email, password: password);
}
英文:

The function createUserWithEmailAndPassword returns indeed a Future containing the UserCredential.

Please make sure that the return type of the function used in the Auth class matches the return type of the function createUserWithEmailAndPassword provided by FirebaseAuth.

It should look like this:

class Auth {
  static Future&lt;UserCredential&gt; createUserWithEmailAndPassword({
    required String email,
    required String password,
  }) =&gt;
      FirebaseAuth.instance
          .createUserWithEmailAndPassword(email: email, password: password);
}

答案2

得分: 1

尝试使用以下代码而不是使用:

FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: _controllerEmail.text,
    password: _controllerPassword.text
);
英文:

instead of using

Auth()..createUserWithEmailAndPassword(
    email: _controllerEmail.text,
    password: _controllerPassword.text
  );

try using

FirebaseAuth.instance.createUserWithEmailAndPassword(
    email: _controllerEmail.text,
    password: _controllerPassword.text
  ); 

huangapple
  • 本文由 发表于 2023年6月19日 20:09:23
  • 转载请务必保留本文链接:https://go.coder-hub.com/76506484.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定