代码之家  ›  专栏  ›  技术社区  ›  BIS Tech

有效的Dart警告避免forEach。改为将内置用于in

  •  0
  • BIS Tech  · 技术社区  · 6 年前

    有效的Dart警告避免forEach。改为将内置用于in。

    Please click this link

    有没有更好的办法?我想 wait return

     List<PromotionModel> allPromotion = //add data from api;
    
     Future<List<PromotionModel>> filterPromotion() async {
        List<PromotionModel> temp = [];
        if(!check){
          return allPromotion;
        }else{
          await Future.forEach(allPromotion, (v) async {
            if(v.vendoruid == uid){
              temp.add(v);
            }
          });
          return temp;
        }
      }
    
    1 回复  |  直到 6 年前
        1
  •  1
  •   Benjamin user12576004    6 年前

    假设 allPromotion 属于 Future<List> 类型,仅 await 未来和循环提供的列表,如下所示:

    final list = await allPromotion;
    for (var v in list) {
      if (v.vendoruid == uid) {
        temp.add(v);
      }
    }
    return temp;