我正在尝试从api中提取数据。 我的地图在null上被调用。我不知道我编写的用于调用json的代码是错误的还是我的模型是错误的。
这是我调用json的方法。
Feature getProducts() async {
http.Response response =
await http.get(CarmanduConstant.baseURL + "catalogue", headers: {
'Accept': 'application/json',
'Content-type': 'application/json',
'Authorization': '$token_type' + ' $access_token'
});
Map<String, dynamic> decodedCategories = json.decode(response.body);
List<ProductPoDo> components = decodedCategories['data'];
print(components);
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Products"),
),
body: new Container(
padding: EdgeInsets.fromLTRB(8.0, 8.0, 8.0, 8.0),
child: Material(
child: buildListItems(getProducts()),
),
),
);
}
}
这是我的模特
class ProductPoDo {
String id;
String title;
String featuredImage;
String excerpt;
String discountedPrice;
final CategoryPoDo category;
ProductPoDo({this.id, this.title, this.featuredImage, this.excerpt,
this.discountedPrice, this.category});
ProductPoDo.fromJson(Map<String, dynamic> json)
: id = json['id'],
title = json['title'],
featuredImage = json['featured_image'],
excerpt = json['excerpt'],
discountedPrice = json['discounted_price'],
category = CategoryPoDo.fromJson(json['CategoryPoDo']);
}
class CategoryPoDo {
String id;
String title;
String description;
CategoryPoDo({this.id, this.title, this.description});
factory CategoryPoDo.fromJson(Map<String, dynamic> json) {
return CategoryPoDo(
id: json['id'], title: json['title'], description:
json['description']);
}
}
这是显示的错误
[ERROR:flutter / lib / ui / ui_dart_state.cc(148)]未处理的异常: NoSuchMethodError:方法“ map”在null上调用。电子/颤振 (23096):接收方:空E / flutter(23096):尝试调用: map(Closure:(dynamic)=> ProductPoDo)E / flutter(23096): 0 Object.noSuchMethod(dart:core-patch / object_patch.dart:50:5)E / flutter(23096):1 _ProductsssssState.getProducts (包装:carmanduuuu / test / producttest.dart:36:20)E / flutter(23096):