0

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;
    }
  }
4
  • 1
    What's the field type of allPromotion? Commented Dec 26, 2019 at 0:56
  • allPromotion type is function type... -> List<PromotionModel> Commented Dec 26, 2019 at 3:19
  • they say about Iterable.forEach not about Future.forEach - btw why do you need Future.forEach in your case? why dont you simply use Iterable.where? Commented Dec 26, 2019 at 7:05
  • @pskink I need to wait until loop finished and then I need to return temp. that's why I used. Is there any other way? Commented Dec 26, 2019 at 7:40

1 Answer 1

1

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;
Sign up to request clarification or add additional context in comments.

3 Comments

please check again my question... I forgot to add type of allPromotion
Maybe I'm misunderstanding but allPromotion isn't of Future type so why are you using Future.forEach()?
otherwise, return an empty value without a complete loop.. isn't it?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.