BIS Tech
BIS Tech

Reputation: 19434

Effective Dart warns about avoiding forEach. Use build in for-in instead

Effective Dart warns about avoiding forEach. Use build in for-in instead.

Please click this link

Is there any better way? I want to wait and 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;
    }
  }

Upvotes: 0

Views: 172

Answers (1)

Benjamin
Benjamin

Reputation: 6161

Assuming that allPromotion is of Future<List> type, just await the future and loop through the provided list like so:

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

Upvotes: 1

Related Questions