简体   繁体   English

如何在循环中使用 Future/Async function? (飞镖/颤振)

[英]how can I use Future/Async function in a loop? (dart/flutter)

        String random_username ;
        bool available = false;
        int n = 0;
        do{
          random_username = SuperHero.random()+(n.toString());
          n = n+1;
          print(random_username);
          checkUsernameAvailable(random_username).then((value){
            available = value;
          });
        }while(!available);

What I am trying to do is when a new user is registered in Firebase Auth, I want to create a random username for the user, which can be changed later.我想做的是当一个新用户在 Firebase Auth 中注册时,我想为该用户创建一个随机用户名,以后可以更改。 this code should generate a username, check if it is already in firebase database.此代码应该生成一个用户名,检查它是否已经在 firebase 数据库中。 If no, then leave the loop, else add a suffix number and retry.如果否,则离开循环,否则添加后缀号并重试。 (By mistake I've put the name generator in the loop, so every time it will get a new name, and that's okay for now). (我错误地将名称生成器放在循环中,所以每次它都会得到一个新名称,现在没关系)。 The function checkUsernameAvailable return "true" if there's no match, ie available.如果没有匹配项,即可用,则 function checkUsernameAvailable返回“true”。 and vice versa.反之亦然。

The problem is that the loop continues forever.问题是循环永远持续下去。 maybe this is due to the async method,ie, checkUsernameAvailable .也许这是由于异步方法,即checkUsernameAvailable Please help.请帮忙。

You can use Future.doWhile() method, which performs an operation repeatedly until it returns false :您可以使用Future.doWhile()方法,该方法重复执行操作,直到它返回false

Future.doWhile(() async {
  final random_username = SuperHero.random() + (n.toString());
  n = n + 1;
  print(random_username);
  return !(await checkUsernameAvailable(random_username));
});

暂无
暂无

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

相关问题 我无法从未来函数中的列表中取出数据。 颤动/飞镖 - I can't take out data from list inside future function. Flutter/Dart 我如何在 map function 中等待我未来的 function? (扑) - How can I wait for my future function in map function? (Flutter) 当我再次切换回选项卡时,如何阻止我未来的构建者重新加载? - 颤振/飞镖 - How can I stop my future builders to reload when I switch back to the tab again? - Flutter/dart flutter 如何在 init state 中使用未来的异步方法 - flutter how to use future async method in init state Flutter Web(测试版)、Dart、无法完成异步 Future 函数 - 使用 JSON Firestore 包装器插件 - asyc、await、Future - Flutter Web (Beta), Dart, Can't get asynchronous Future function to finish - with JSON Firestore wrapper plugin - asyc, await, Future 在工厂运行异步代码 Function 在 Flutter / Dart - Running Async Code in factory Function in Flutter / Dart 无法转换 Future<String> 字符串并在 Dart (Flutter) 中使用 - Cannot convert Future<String> to String and use in Dart (Flutter) 如何在 firestore 中使用 .where() 查询在不同屏幕之间导航? Flutter, Dart, Firebase - How can I use the .where() query in firestore to navigate between different screens? Flutter, Dart, Firebase 如何使用云 function auth 触发颤振/飞镖 - How to use cloud function auth trigger flutter/dart 未来飞镖/颤振的返回值 - Return value in Future dart/flutter
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM