Flutter - 未处理的异常:“String”类型不是“index”的“int”类型的子类型

All*_*son 7 dart flutter

我正在尝试获取 api,我可以在控制台中打印 api 响应。但是,当我收到对模型类的响应时,控制台显示如下错误

E/flutter(9292):[错误:flutter/lib/ui/ui_dart_state.cc(157)]未处理的异常:类型“String”不是“index”的“int”类型的子类型

模型.dart

class CategoryDishes {
  final String dishId;
  final String dishName;
  final String dishDescription;

  CategoryDishes(
      {this.dishId,
      this.dishName,
      this.dishDescription,});

  factory CategoryDishes.fromJson(Map<String, dynamic> json) {
    return CategoryDishes(
        dishId: json['dish_id'],
        dishName: json['dish_name'],
        dishDescription: json['dish_description'],

  }

  static Resource<List<CategoryDishes>> get all {
    return Resource(
        url: Constants.FOOD_API_URL,
        parse: (response) {
          final result = json.decode(response.body.toString());
          print(response);
          Iterable list = result['category_dishes'];
          return list.map((model) => CategoryDishes.fromJson(model)).toList();
        });
  }
}
Run Code Online (Sandbox Code Playgroud)

web_service.dart

class Resource<T> {
  final String url;
  T Function(Response response) parse;

  Resource({this.url, this.parse});
}

class Webservice {
  Future<T> load<T>(Resource<T> resource) async {
    final response = await http.get(resource.url);
    if (response.statusCode == 200) {
      debugPrint("----D------>" + response.body);
      return resource.parse(response);
    } else {
      throw Exception('Failed to load data!');
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

在控制台中显示debugPrintapi,而且还显示上述错误,并且 api 数据未显示在我创建的视图上。

我做错了什么?

任何建议都会有帮助。

Nav*_*idi 3

API 返回 JSON 数组!尝试如下所示!

static Resource<List<CategoryDishes>> get all {
    return Resource(
        url: Constants.FOOD_API_URL,
        parse: (response) {
            final result = json.decode(response.body.toString());
            print(response);
        //added 0 indexex, so it gets 1st element of JSON Arrays
            Iterable list = result[0]['table_menu_list'][0]['category_dishes'];
            return list.map((model) => CategoryDishes.fromJson(model)).toList();
        });
    }
}
Run Code Online (Sandbox Code Playgroud)