【问题标题】:Flutter - Unhandled Exception: type 'String' is not a subtype of type 'int' of 'index'Flutter - 未处理的异常:类型'String'不是'index'类型'int'的子类型
【发布时间】:2020-04-19 22:28:50
【问题描述】:

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

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

model.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();
        });
  }
}

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!');
    }
  }
}

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

我做错了什么?

任何建议都会有所帮助。

【问题讨论】:

  • 你能发布你的json数据格式吗?

标签: flutter dart


【解决方案1】:

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();
                });
          }

}

【讨论】:

  • E/flutter (25339): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method 'map' was called on null. E/flutter (25339): Receiver: null E/flutter (25339): Tried calling: map&lt;CategoryDishes&gt;(Closure: (dynamic) =&gt; CategoryDishes) E/flutter (25339): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:53:5) E/flutter (25339): #1 CategoryDishes.all.&lt;anonymous closure&gt; (package:test_flutter_api/model/api_model.dart:54:23) - 现在显示错误,映射错误
  • [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: type '_InternalLinkedHashMap&lt;String, dynamic&gt;' is not a subtype of type 'Iterable&lt;dynamic&gt;'- 迭代响应时出现问题
  • 使用 Map 而不是 List 的 Iterable ! Like => 地图列表= 结果[0]['table_menu_list'][0]['category_dishes'][0];
  • Y 我不能使用可迭代列表。上面你贴的代码没问题,upvoted() 的回报是什么;
  • 因为你的错误说你正在使用 iterable 而不是 Internal.......Map 并确保数组值在哪里是输出使用索引
【解决方案2】:

当 API 返回一个我们不能使用的数组时

 result['category_dishes'] 

我们直接使用结果,因为它是一个列表

List list= result ;
list.map((i) =>.....

我遇到了同样的错误,这就是我的解决方案。

【讨论】:

    【解决方案3】:

    我从 firebase 收到此错误,因为我没有将字段值放在引号中。必须将它们放在引号中才能正确工作。

    products.json?auth=token&orderBy=CreatorID <- Error
    

    只需将字段值放在引号中

    products.json?auth=token&orderBy="CreatorID" <- Resolved
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2022-09-22
      • 2021-07-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-09-19
      相关资源
      最近更新 更多