json 循环的Dart,未完成循环

ghhkc1vu  于 2023-02-01  发布在  其他
关注(0)|答案(1)|浏览(100)

嘿,我有这个for循环,for循环执行后,确保响应是有效的,关键餐厅有值,索引仅用于调试目的,我没有revising任何返回值(无限加载屏幕)也尝试

for (int i = 0; i <= jsonResponse['Restaurants'].length; i++)

但是i从来没有达到长度并完成循环,我认为问题可能是在添加到列表时,
所有值均已正确提取

int index = 0;
  for (var restaurant in jsonResponse['Restaurants']) {
    var restaurantName = restaurant['Name'];
    if (!hiddenRestaurants.contains(restaurantName)) {
      var restaurantOnline = restaurant['IsOpenNow'];
      if (restaurantOnline) {
        var restaurantImage = restaurant['LogoUrl'];
        var restaurantAddress = restaurant['Address']['FirstLine'] +
            ", " +
            restaurant['Address']['City'];    
        var restaurantDeliveryPrice = restaurant['DeliveryCost'];
        var restaurantDeliveryInt = restaurant['DeliveryCost'];

        var restaurantDeliveryETA =
            "${restaurant['DeliveryEtaMinutes']['RangeLower']} - ${restaurant['DeliveryEtaMinutes']['RangeUpper']}";
        var restaurantETA = restaurant['DeliveryWorkingTimeMinutes'];

        var restaurantTags = [];
        for (var tag in restaurant['Cuisines']) {
          restaurantTags.add(tag['Name']);
        }

        var restaurantRating = (restaurant['RatingStars'] * 2.0) ?? 0.0;

        var loc = restaurant['Id'];
        restaurants.add(Restaurant(
            title: restaurantName,
            image: restaurantImage,
            address: restaurantAddress,
            tags: restaurantTags,
            loc: loc,
            slug: restaurantName.toString().replaceAll(" ", "-"),
            service: DeliveryService.tenbis,
            deliveryPrice: restaurantDeliveryPrice,
            deliveryInt: restaurantDeliveryInt,
            estimateRange: restaurantDeliveryETA,
            online: restaurantOnline,
            eta: restaurantETA,
            country: "",
            score: restaurantRating,
            priceRange: null));
      }
    }
    print(++index);
  }
  return restaurants;
46qrfjad

46qrfjad1#

为什么你要用这么奇怪的方法呢?请查看这个文档https://docs.flutter.dev/development/data-and-backend/json,我希望它能帮助你。首先从json获得模型,然后用model/models做你想做的事情会更方便。

相关问题