我正在尝试按ID单独输出产品库。但是我得到一个错误Map不是可迭代类型的子类型。我仍然是新手,正在这里学习。
这是我的API,我认为错误来自这里。谢谢您的提前帮助。
Future<List<Product>> fetchProduct(String productID) async{
String productPost = ApiUtil.productPost(productID);
Map<String, String> headers = {
'Accept' : 'application/json'
};
var response = await http.get(productPost, headers: headers);
List<Product> productpost = [];
if( response.statusCode == 200 ) {
Map<String, dynamic> body = json.decode(response.body);
for ( var item in body['data'] ){
Product product = Product.fromJson(item);
productpost.add(product);
}
}
return productpost;
}
这是我的模特。
class Product {
String id, name, description;
Product(this.id, this.name, this.description);
Product.fromJson( Map<String,dynamic> jsonObject ){
this.id = jsonObject['id'].toString();
this.name = jsonObject['name'];
this.subname = jsonObject['description'];
}
}
这是我的详细信息类。
class ProductDetails extends StatefulWidget{
final String productID;
ProductDetails(this.productID);
@override
ProductDetailsState createState() => new ProductDetailsState();
class ProductDetailsState extends State<ProductDetails> {
ProductApi productapi = ProductApi();
@override
void initState() {
super.initState();
}
.... return Scaffold ...
Container(
child: FutureBuilder(
future: productapi.fetchProduct(widget.productID),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if( snapshot.hasError ) {
return Text ('NO DATA');
}
if( snapshot.hasData) {
return _productDetail( snapshot.data );
}
return null;
},
)
)
_productDetail( List<Product> product ){
return PageView.builder(
itemCount: product.length,
itemBuilder: ( BuildContext context, int index ){
return new Padding(
padding: EdgeInsets.all(10.0),
child: Column(
mainAxisSize: MainAxisSize.max,
crossAxisAlignment: CrossAxisAlignment.stretch,
children: <Widget>[
Text(product[index].name, textAlign: TextAlign.center, style:
TextStyle(fontSize: 28, color: Colors.black, fontWeight: FontWeight.w700)),
]
)
);
}
);
}
我正在尝试通过在线搜索参考文献来学习和理解,但是仍然有些问题我看不出来。
这是我从控制台收到的错误
[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The getter 'iterator' was called on null.
Receiver: null
Tried calling: iterator
#0 Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1 ProductApi.fetchProduct (package:tampr/api/product_api.dart:61:29)
<asynchronous suspension>
#2 ProductDetailsState.initState (package:tampr/pages/product_details.dart:26:16)
#3 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4068:58)
#4 ComponentElement.mount (package:flutter/src/widgets/framework.dart:3919:5)
#5 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3101:14)
#6 Element.updateChild (package:flutter/src/widgets/framework.dart:2904:12)
#7 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5127:14)
#8 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3101:14)
#9 Element.updateChild (package:flutter/src/widgets/framework.dart:2904:12)
#10 <…>
JSON数据
{"data":[{
"id": 1,
"name": "Product Name",
"description": "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.",
"productimage": "product_img.jpg",
"logo": "logo_img.png",
"user_id": 1,
"created_at": "2019-11-18 05:25:49",
"updated_at": "2019-11-18 05:25:49",
"options": [
{
"id": 1,
"name": "option name",
"image": "optionimage.jpg",
"pivot": {
"product_id": 1,
"option_id": 1
}
},
]
}]}
答案 0 :(得分:1)
就我而言,我忘记检查列表中的项目是否为空。下面的代码帮助我从同样的错误中恢复
if (filterRestaurantList[index].category != null) {
for (var category
in filterRestaurantList[index].category) {
if (category.categoryDetail != null) {
categoryList.add(category.categoryDetail.categoryName);
}
}
答案 1 :(得分:0)
我发现我的API for loop问题。通过删除它可以解决问题。由于获取器是按productID生成的,因此它仅返回一项。
Future<List<Product>> fetchProduct(String productID) async{
String productPost = ApiUtil.productPost(productID);
Map<String, String> headers = {
'Accept' : 'application/json'
};
var response = await http.get(productPost, headers: headers);
List<Product> productpost = [];
if( response.statusCode == 200 ) {
Map<String, dynamic> body = json.decode(response.body);
Product product = Product.fromJson(item);
productpost.add(product);
}
}return productpost;
答案 2 :(得分:0)
我也遇到了同样的问题,在我看来,这是由于:
Product product = Product.fromJson(item); 来自Firestore的[item]结构与[fromJson]解码结构不匹配 或其中之一是字段命名中的null或拼写错误。
我的错误是扑朔迷离,但这没关系。