【问题标题】:My flutter code is not reading the code in the order that I expect it to do it我的颤动代码没有按照我期望的顺序读取代码
【发布时间】:2021-07-19 18:54:10
【问题描述】:

这3个函数在一个类中,我使用它们的顺序是下一个:

  1. 调用登录查询。

  2. 如果密码与用户名匹配,它将调用函数loginAccepted()

  3. 此函数将类变量的值更改为 1,表示输入的数据是正确的。

到目前为止,代码按他们的方式工作。现在问题来了。

我有一个ElevatedButton,其中我:

  1. 从其他类调用loginQuery 函数。

  2. 如果userMaster.loginVerification 为 1,这意味着数据是正确的,它将显示一个弹出窗口,说明它是正确的。 (userMaster 是我用来控制该类功能的变量。)

  3. 如果loginVerification 不是 1,它也会显示一个弹出窗口来说明它。

然后,当我尝试我的代码输入有效的用户名和密码时,它会在左侧显示,控制台在控制台中显示确认数据是有效用户的打印,然后在右侧显示程序显示弹出窗口,就好像我输入的数据不是有效用户一样。

然后我在控制台中输入 userMaster.loginVerification 以检查发生了什么,它显示一个 1,这意味着我输入的数据是有效的,但弹出窗口中的该值是 0,这没有任何意义。

我不明白我的代码发生了什么,我开始相信函数是在 ElevatedButton 或其他东西中执行的最后一件事,任何帮助将不胜感激。

【问题讨论】:

    标签: firebase function flutter class dart


    【解决方案1】:

    这实际上是this question,但它是面向 JS 的,你会感到困惑。不过,这只是 async 编程。

    1. loginQuery返回一个Future
    Future<void> loginQuery() async {
      var docs = await Firestore.instance.collection(...).where(...).getDocuments();
      // existing if statement
      if(docs.documents.isEmpty) {
       ...
      } else {
       ...
      }
    }
    
    1. awaitonPressed 中的未来
    onPressed:() async {
      await userMaster.loginQuery(...);
      // now do your if check
    }
    

    【讨论】:

    • 我添加了您建议的异步内容,但它仍然没有工作,它实际上具有完全相同的结果,控制台显示验证已正确完成,但弹出窗口没有及时获得数字来显示它。
    • @MartinPizarro - 发布您更新的代码(也不是图片)。
    • 我用更新后的代码回答了我的问题。
    【解决方案2】:

    类中的函数就是这样结束的:

    Future<void> loginQuery(String user, String password) async {
    Firestore.instance
        .collection('Usuarios')
        .where('usuario', isEqualTo: user)
        .getDocuments()
        .then((QuerySnapshot docs) {
      if (docs.documents.isNotEmpty) {
        var w = docs.documents[0].data;
    
        if (w['contraseña'] == password) {
          print('Datos verificados correctamente');
          loginAccepted();
        } else {
          print('El usuario no existe o la contraseña no corresponde');
          loginDeclined();
        }
      }
    });}
    

    按钮就是这样结束的:

    ElevatedButton(
            onPressed: () async {
              await userMaster.loginQuery(
                  loginUserController.text, loginPasswordController.text);
              if (userMaster.loginVerification == 1) {
                showDialog(
                    context: context,
                    builder: (_) => AlertDialog(
                          title: Text('Inicio de sesion correcto.'),
                        ),
                    barrierDismissible: true);
              } else {
                Navigator.pop(context);
                showDialog(
                    context: context,
                    builder: (_) => AlertDialog(
                          title: Text('Credenciales incorrectas.'),
                        ),
                    barrierDismissible: true);
              }
    
              // Navigate back to first route when tapped.
            },
            child: Text('Ingresar'),
          ),
    

    【讨论】:

    • 没有照我说的做,在loginQuery中使用async/await。您需要var docs = await Firestore.instance().collection(...).where(...).getDocuments()
    • 我没有意识到 docs = await 部分,现在我更改了它并使其正常工作,非常感谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-07-08
    • 1970-01-01
    • 2017-09-01
    • 2018-04-30
    • 1970-01-01
    • 2012-10-05
    • 2011-06-18
    相关资源
    最近更新 更多