简体   繁体   English

对于和如果在 flutter

[英]For and If in flutter

I have this code我有这个代码

String passou = 'true';
for (int j = 0; j < items.length; j++) {
      verifyAllProd(items[j].ref).then(
      (quanti) {
        if (double.parse(quanti) <= 0) {
           passou = 'false';
           showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: const Text("Mensagem"),
                content: Text("O produto ${items[j].name} já nao está disponivel"),
               );
             },
           );
         }
        if (double.parse(quanti) < items[j].quantity &&
            double.parse(quanti) != 0) {
             passou = 'false';                
             showDialog(
             context: context,
             builder: (context) {
                  return AlertDialog(
                     title: const Text("Mensagem"),
                     content: Text("O produto ${items[j].name} apena tem disponivel: $quanti"),
            );
          },
        );
      }
    },
  );
}
print(passou);
if (passou == 'true') {
     Navigator.push(
     context,
     MaterialPageRoute(
     builder: (context) => const ListPaymentTrans(),
    ),
  );
}

I don't know why but when I use this code in my app the "if" is called first than "for".我不知道为什么,但是当我在我的应用程序中使用此代码时,首先调用“if”而不是“for”。 That is, I click on the button and instead of the "if" wait for the "for" to be asked to see if the "passou" will receive the value false.也就是说,我单击按钮而不是“if”等待“for”被要求查看“passou”是否会收到值 false。 It does the "if" right away and doesn't wait for the "for" to finish (opens the page).它立即执行“if”,而不等待“for”完成(打开页面)。

I don't know if there is any way to change this?不知道有什么办法可以改变吗?

You are doing it the wrong way.你做错了。 Try this instead of using boolean as string use it as boolean.试试这个,而不是将 boolean 用作字符串,而是将其用作 boolean。

    bool passou = false;
for (int j = 0; j < items.length; j++) {
      verifyAllProd(items[j].ref).then(
      (quanti) {
        if (double.parse(quanti) <= 0) {
           passou = false;
           showDialog(
            context: context,
            builder: (context) {
              return AlertDialog(
                title: const Text("Mensagem"),
                content: Text("O produto ${items[j].name} já nao está disponivel"),
               );
             },
           );
         }
        if (double.parse(quanti) < items[j].quantity &&
            double.parse(quanti) != 0) {
             passou = false;                
             showDialog(
             context: context,
             builder: (context) {
                  return AlertDialog(
                     title: const Text("Mensagem"),
                     content: Text("O produto ${items[j].name} apena tem disponivel: $quanti"),
            );
          },
        );
      }
    },
  );
}
print(passou);
if (passou) {
     Navigator.push(
     context,
     MaterialPageRoute(
     builder: (context) => const ListPaymentTrans(),
    ),
  );
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM