我是flutter的新手,我正在使用ListView.Builder构建一个listView,这里有我的模型类,并且我确实发布了标题数据,但它总是返回null,我已经尝试了所有可能但不知道为什么正在发生,那里有任何解决方案,这可能真的很有帮助..
数据.dart
class Data {
String itemId;
String name;
String price;
String picture;
String description;
String status;
Data({this.itemId,
this.name,
this.price,
this.picture,
this.description,
this.status});
Data.fromJson(Map<String, dynamic> json) {
itemId = json['item_id'];
name = json['name'];
price = json['price'];
picture = json['picture'];
description = json['description'];
status = json['status'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['item_id'] = this.itemId;
data['name'] = this.name;
data['price'] = this.price;
data['picture'] = this.picture;
data['description'] = this.description;
data['status'] = this.status;
return data;
}
}
我的 Listview 课程是
ListItems.dart
import 'dart:convert';
import 'package:boxmi_now/model/Data.dart';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:http/http.dart' as http;
import 'ItemView.dart';
class ListItemsSuman extends StatefulWidget {
@override
_ListItemsSumanState createState() => _ListItemsSumanState();
}
class _ListItemsSumanState extends State<ListItemsSuman> {
List<Data> listItem = [];
var loading = false;
Future<Null> getData() async {
setState(() {
loading = true;
});
SharedPreferences user = await SharedPreferences.getInstance();
// ignore: non_constant_identifier_names
var user_id = user.getString('user_id');
// ignore: non_constant_identifier_names
var access_token = user.getString('access_token');
Map data = {
"user_id": user_id,
"access_token": access_token,
};
var url = My_HTTP_URL;
final responseData = await http.post(url, body: data);
final decodeData = jsonDecode(responseData.body);
print(decodeData);
setState(() {
//here is whey i remains null
for (Map i in decodeData) {
listItem.add(Data.fromJson(i));
}
loading = false;
});
}
@override
void initState() {
// TODO: implement initState
super.initState();
asyncMethod();
}
Future<void> asyncMethod() async {
await getData();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Container(
child: loading ? Center(child: CircularProgressIndicator()) :
ListView.builder(
itemCount: listItem.length,
itemBuilder: (context, i) {
final nDataList = listItem[i];
return Container(
child: InkWell(
onTap: () {
Navigator.push(
context, MaterialPageRoute(builder: (context) =>
ItemView(
)));
},
child: Card(
child: Padding(
padding: EdgeInsets.all(10.0),
child: Column(
children: [
Text(nDataList.name),
Text(nDataList.price),
Container(
height: 100.0,
width: 100.0,
child: Image.network(nDataList.picture),
),
],
),
),
),
),
);
}
),
),
);
}
}
我的解码 JSON 响应是由
{"success":true,
"data":[
{
"item_id":"UID-60279bce6f25d161320852660279bce6f261",
"name":"Fish 1",
"price":"\u20b9100.00",
"picture":"ea8ccfeb468955a9a1b4f82377f59298ce368209.9j",
"description":"This is fish. And nice one",
"status":"1"}
,
{
"item_id":"UID-60279bc1d63a3161320851360279bc1d63a9",
"name":"Fish 2",
"price":"\u20b9100.00",
"picture":"f1c8601cbc9ff0940f4a5fc8d3c1f608cf460de0.9j",
"description":"This is fish. And nice one",
"status":"1"}
]
,"code":200}
在控制台中,我得到这样的
E/flutter (15241): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: type '_InternalLinkedHashMap<String, dynamic>' is not a subtype of type 'Iterable<dynamic>'
E/flutter (15241): #0 _ListItemsSumanState.getData.<anonymous closure> (package:boxmi_now/Screens/ListItemsSuman.dart:40:21)
E/flutter (15241): #1 State.setState (package:flutter/src/widgets/framework.dart:1267:30)
E/flutter (15241): #2 _ListItemsSumanState.getData (package:boxmi_now/Screens/ListItemsSuman.dart:39:5)
E/flutter (15241): <asynchronous suspension>
E/flutter (15241): #3 _ListItemsSumanState.asyncMethod (package:boxmi_now/Screens/ListItemsSuman.dart:55:5)
E/flutter (15241): <asynchronous suspension>
E/flutter (15241):