在循环上运行的notifyListeners()

时间:2019-09-01 08:27:01

标签: android flutter dart visual-studio-code

Notifylisteners不断循环运行

Future<void> fetchAndSetMeal() async {
final List<Meal> loadedProducts = [];
var data = await DataBaseHelpers.getData();
if (data.documents == null) {
  return;
}
List<DocumentSnapshot> list = data.documents;

list.forEach((document) async {
  var url = await DataBaseHelpers.getImageUrl(document.documentID);

  loadedProducts.insert(
      0,
      Meal(
          id: document.documentID,
          category: document.data['category'],
          description: document.data['description'],
          title: document.data['title'],
          price: document.data['price'],
          imgUrl: url));
});
await Future.delayed(Duration(milliseconds: 300));
print(loadedProducts[0]);
notifyListeners();
_items = loadedProducts;

} }

/// print和network调用语句也运行两次,而不是//每个调用一次。 notifyListeners()正在循环运行;

1 个答案:

答案 0 :(得分:0)

您将notifyListeners放入了foreach循环中。将其移到外面