gpt4 book ai didi

android - flutter 错误 : The getter 'iterator' was called on null

转载 作者:行者123 更新时间:2023-12-04 13:00:15 25 4
gpt4 key购买 nike

我正在尝试按 id 单一输出产品库。但是我得到一个错误 Map is not a subtype of type iterable。我还是新手,我有点卡在这里。

这是我的 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)。
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
}
},
]
}]}

最佳答案

我也遇到了同样的问题,就我而言,这是因为:

Product product = Product.fromJson(item);[item] structure coming from Firestore doesn't match [fromJson] decoding structureor maybe one of them is null or typo error in the field naming.


我的错误在于 flutter ,但这没关系。

关于android - flutter 错误 : The getter 'iterator' was called on null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58954923/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com