Mysql 颤振:将json实现为list返回空列表
您好,我正在制作一个新的颤振应用程序,我是颤振新手,我正在使用mysql数据库,我得到的响应是json。 以下是我的格式化json响应: JsonResponseMysql 颤振:将json实现为list返回空列表,mysql,json,flutter,Mysql,Json,Flutter,您好,我正在制作一个新的颤振应用程序,我是颤振新手,我正在使用mysql数据库,我得到的响应是json。 以下是我的格式化json响应: JsonResponse { "folders":[ { "id":2107, "name":"Labs ", "img":null, "haschild&quo
{
"folders":[
{
"id":2107,
"name":"Labs ",
"img":null,
"haschild":1,
"parentid":1955,
"createdate":"2019-09-30 03:00:39",
"inarchive":0,
"active":1
},
{
"id":2108,
"name":"L1",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-09-30 03:00:45",
"inarchive":0,
"active":1
},
{
"id":2130,
"name":"L2",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-02
02:39:11",
"inarchive":0,
"active":1
},
{
"id":2175,
"name":"L3",
"img":null,
"haschild":1,
"parentid":1955,
"createdate":"2019-10-07 03:43:15",
"inarchive":0,
"active":1
},
{
"id":2202,
"name":"L4",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-09 02:08:10",
"inarchive":0,
"active":1
},
{
"id":2227,
"name":"\u0645\u0631\u0627\u062c\u0639\u0627\u062a
",
"img":null,
"haschild":1,
"parentid":1955,
"createdate":"2019-10-11 05:15:43",
"inarchive":0,
"active":1
},
{
"id":2242,
"name":"L5",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-14 03:45:24",
"inarchive":0,
"active":1
},
{
"id":2257,
"name":"L6",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-16
06:23:41",
"inarchive":0,
"active":1
},
{
"id":2269,
"name":"L7",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-21 03:39:12",
"inarchive":0,
"active":1
},
{
"id":2309,
"name":"L8",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-23 05:14:48",
"inarchive":0,
"active":1
},
{
"id":2310,
"name":"L8",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-23
05:14:51",
"inarchive":0,
"active":1
},
{
"id":2346,
"name":"L9",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-28 04:22:24",
"inarchive":0,
"active":1
},
{
"id":2370,
"name":"L10",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-10-30 03:02:00",
"inarchive":0,
"active":1
},
{
"id":2446,
"name":"L11",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-12
06:28:13",
"inarchive":0,
"active":1
},
{
"id":2462,
"name":"\u062a\u0628\u064a\u0636\u0627\u062a",
"img":null,
"haschild":1,
"parentid":1955,
"createdate":"2019-11-13 08:39:00",
"inarchive":0,
"active":1
},
{
"id":2464,
"name":"L12",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-13
08:40:55",
"inarchive":0,
"active":1
},
{
"id":2488,
"name":"L13",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-18 06:09:11",
"inarchive":0,
"active":1
},
{
"id":2490,
"name":"L14",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-18 06:38:59",
"inarchive":0,
"active":1
},
{
"id":2491,
"name":"L15",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-18
06:45:57",
"inarchive":0,
"active":1
},
{
"id":2497,
"name":"\u062a\u0644\u062e\u064a\u0635\u0627\u062a",
"img":null,
"haschild":1,
"parentid":1955,
"createdate":"2019-11-19 04:24:49",
"inarchive":0,
"active":1
},
{
"id":2509,
"name":"L16",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-20
09:56:04",
"inarchive":0,
"active":1
},
{
"id":2522,
"name":"L17",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-25 06:38:27",
"inarchive":0,
"active":1
},
{
"id":2542,
"name":"L18",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-27 12:19:12",
"inarchive":0,
"active":1
},
{
"id":2552,
"name":"Protein
Lab",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-27 16:32:27",
"inarchive":0,
"active":1
},
{
"id":2562,
"name":"Chromatography ",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-11-30 15:24:24",
"inarchive":0,
"active":1
},
{
"id":2576,
"name":"L19",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-02
02:05:29",
"inarchive":0,
"active":1
},
{
"id":2580,
"name":"L20",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-02 08:23:48",
"inarchive":0,
"active":1
},
{
"id":2592,
"name":"21",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-03 06:27:24",
"inarchive":0,
"active":1
},
{
"id":2600,
"name":"L22",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-04
06:22:46",
"inarchive":0,
"active":1
},
{
"id":2614,
"name":"L23",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-04 10:03:00",
"inarchive":0,
"active":1
},
{
"id":2640,
"name":"L24",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-08 06:53:11",
"inarchive":0,
"active":1
},
{
"id":2644,
"name":"L25",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-09
05:33:07",
"inarchive":0,
"active":1
},
{
"id":2649,
"name":"L26",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-09 07:58:42",
"inarchive":0,
"active":1
},
{
"id":2711,
"name":"\u0623\u0633\u0626\u0644\u0629",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-12-19
05:18:11",
"inarchive":0,
"active":1
}
],
"files":[
{
"id":4542,
"name":"Ch2-part1.m4a 27.52MB",
"img":null,
"uploader":"Aws",
"url":"http:\/\/msc-mu.com\/..\/uploaded\/031020190Ch2-part1.m4a",
"createdate":"2019-10-03 09:53:39",
"approved":1,
"active":1
}
]
}
每当我尝试实现文件夹和文件的列表时,它都会说列表中没有任何内容,并且是空的
这是我的密码:
同伴飞镖
class SecondScreen extends StatefulWidget {
final YearsMain value;
SecondScreen({Key key, this.value}) : super(key: key);
@override
_SecondScreenState createState() => _SecondScreenState();
}
class _SecondScreenState extends State<SecondScreen> {
Future<List<Folders>> getData() async {
List<Folders> list;
String link =
"http://msc-mu.com/api_verfication.php";
var res = await http.post(link,body: {
'flag':'selectfellowsubjects',
'year':widget.value.id
});
print(res.body);
if (res.statusCode == 200) {
var data = json.decode(res.body);
var rest = data["folders"] as List;
print(rest);
list = rest.map<Folders>((json) => Folders.fromJson(json)).toList();
}
print("List Size: ${list.length}");
return list;
}
Widget listViewWidget(List<Folders> article) {
return Container(
child: ListView.builder(
itemCount: 20,
padding: const EdgeInsets.all(2.0),
itemBuilder: (context, position) {
return Card(
child: ListTile(
title: Text(
'${article[position].name}',
style: TextStyle(
fontSize: 18.0,
color: Colors.black,
fontWeight: FontWeight.bold),
),
onTap: () => _onTapItem(context, article[position]),
),
);
}),
);
}
void _onTapItem(BuildContext context, Folders folders) {
Navigator.of(context).push(MaterialPageRoute(
builder: (BuildContext context) => DownloadPage()));
}
@override
void initState() {
// TODO: implement initState
super.initState();
print(widget.value.id);
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: new FutureBuilder<List<Folders>>(
future: getData(),
builder: (context, snapshot) {
if (snapshot.hasData) {
List fellow = snapshot.data;
return listViewWidget(fellow);
} else if (snapshot.hasError) {
return Text('${snapshot.error}');
}
return CircularProgressIndicator();
},
),
),
);
}
}
有什么帮助吗?Future getData()异步{
Future<List<Folders>> getData() async {
List<Folders> list= <Folders>[];
String link = "msc-mu.com/api_verfication.php";
try {
var res = await http.post(link,
body: {'flag': 'selectfellowsubjects', 'year': widget.value.id});
print(res.body);
if (res.statusCode == 200) {
var data = json.decode(res.body);
var rest = data["folders"].forEach((v) {
list.add(Folders.fromJson(v));
});
print(rest);
}
return list;
} catch (e) {
debugPrint('$e');
}
}
列表=[];
String link=“msc mu.com/api\u verfication.php”;
试一试{
var res=wait http.post(链接,
正文:{'flag':'selectfellowsubjects','year':widget.value.id});
印刷品(正文);
如果(res.statusCode==200){
var data=json.decode(res.body);
var rest=data[“folders”].forEach((v){
list.add(Folders.fromJson(v));
});
打印(其余);
}
退货清单;
}捕获(e){
debugPrint(“$e”);
}
}
类文件和文件夹模型{
列出文件夹;
列出文件;
FilesAndFoldersModel({this.folders,this.files});
FilesAndFoldersModel.fromJson(映射json){
如果(json['folders']!=null){
文件夹=新列表();
json['folders'].forEach((v){
folders.add(newfolders.fromJson(v));
});
}
如果(json['files']!=null){
文件=新列表();
json['files'].forEach((v){
add(newfiles.fromJson(v));
});
}
}
映射到JSON(){
最终地图数据=新地图();
如果(this.folders!=null){
data['folders']=this.folders.map((v)=>v.toJson()).toList();
}
如果(this.files!=null){
data['files']=this.files.map((v)=>v.toJson()).toList();
}
返回数据;
}
}
类文件夹{
int-id;
字符串名;
空img;
他有孩子;
int-parentid;
字符串createdate;
内部分生组织;
int-active;
文件夹(
{this.id,
这个名字,
这个.img,,
这是我的孩子,
这个.parentid,
这个.createdate,
这是伊纳奇夫,
这是主动的});
Folders.fromJson(映射json){
id=json['id'];
name=json['name'];
img=json['img'];
haschild=json['haschild'];
parentid=json['parentid'];
createdate=json['createdate'];
inarchive=json['inarchive'];
active=json['active'];
}
映射到JSON(){
最终地图数据=新地图();
数据['id']=this.id;
数据['name']=this.name;
数据['img']=this.img;
数据['haschild']=this.haschild;
数据['parentid']=this.parentid;
数据['createdate']=this.createdate;
数据['inarchive']=this.inarchive;
数据['active']=this.active;
返回数据;
}
}
类文件{
int-id;
字符串名;
空img;
字符串上传器;
字符串url;
字符串createdate;
int批准;
int-active;
档案(
{this.id,
这个名字,
这个.img,,
这个,上传者,
这个.url,
这个.createdate,
这是我批准的,
这是主动的});
Files.fromJson(映射json){
id=json['id'];
name=json['name'];
img=json['img'];
uploader=json['uploader'];
url=json['url'];
createdate=json['createdate'];
approved=json['approved'];
active=json['active'];
}
映射到JSON(){
最终地图数据=新地图();
数据['id']=this.id;
数据['name']=this.name;
数据['img']=this.img;
数据['uploader']=此.uploader;
数据['url']=this.url;
数据['createdate']=this.createdate;
数据['approved']=本文件已批准;
数据['active']=this.active;
返回数据;
}
}
在建立这样的模型之后,您应该执行空检查,以检查每个值是否都有一个值。Hi!您可以尝试data[“folders”].forEach((v){list.add(folders.fromJson(v));})`Future getData()异步{List List;String link=”“;var res=wait http.post(链接,正文:{'flag':'selectfellowsubjects','year':widget.value.id});打印(res.body);if(res.statusCode==200){var data=json.decode(res.body);var rest=data[“folders”].forEach((v){List.add)(Folders.fromJson(v));});print(rest);list=rest.map((json)=>Folders.fromJson(json)).toList();}return list;}`如果您的意思是这样的话,它就不起作用了,返回列表的方法与返回null行列表=rest.map((json)=>Folders.fromJson(json)).toList()相同;你能在答案中写出函数的完整代码吗?类型不是类型的子类型,你可以再试一次并告诉我更多详细信息!我们可以联系discordill吗?将其作为答案放置我需要帮助…对不起!这是我的错你只是将debugPrint(e)更改为debugPrint(“$e”)。您能告诉我下一步要做什么吗?或者更新您的答案以在ui及其功能中实现它吗?您现在可以通过创建模型的实例来访问该模型。一旦创建了模型,我建议您调用该模型并对其进行空值检查。
Future<List<Folders>> getData() async {
List<Folders> list= <Folders>[];
String link = "msc-mu.com/api_verfication.php";
try {
var res = await http.post(link,
body: {'flag': 'selectfellowsubjects', 'year': widget.value.id});
print(res.body);
if (res.statusCode == 200) {
var data = json.decode(res.body);
var rest = data["folders"].forEach((v) {
list.add(Folders.fromJson(v));
});
print(rest);
}
return list;
} catch (e) {
debugPrint('$e');
}
}
class FilesAndFoldersModel {
List<Folders> folders;
List<Files> files;
FilesAndFoldersModel({this.folders, this.files});
FilesAndFoldersModel.fromJson(Map<String, dynamic> json) {
if (json['folders'] != null) {
folders = new List<Folders>();
json['folders'].forEach((v) {
folders.add(new Folders.fromJson(v));
});
}
if (json['files'] != null) {
files = new List<Files>();
json['files'].forEach((v) {
files.add(new Files.fromJson(v));
});
}
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
if (this.folders != null) {
data['folders'] = this.folders.map((v) => v.toJson()).toList();
}
if (this.files != null) {
data['files'] = this.files.map((v) => v.toJson()).toList();
}
return data;
}
}
class Folders {
int id;
String name;
Null img;
int haschild;
int parentid;
String createdate;
int inarchive;
int active;
Folders(
{this.id,
this.name,
this.img,
this.haschild,
this.parentid,
this.createdate,
this.inarchive,
this.active});
Folders.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
img = json['img'];
haschild = json['haschild'];
parentid = json['parentid'];
createdate = json['createdate'];
inarchive = json['inarchive'];
active = json['active'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['img'] = this.img;
data['haschild'] = this.haschild;
data['parentid'] = this.parentid;
data['createdate'] = this.createdate;
data['inarchive'] = this.inarchive;
data['active'] = this.active;
return data;
}
}
class Files {
int id;
String name;
Null img;
String uploader;
String url;
String createdate;
int approved;
int active;
Files(
{this.id,
this.name,
this.img,
this.uploader,
this.url,
this.createdate,
this.approved,
this.active});
Files.fromJson(Map<String, dynamic> json) {
id = json['id'];
name = json['name'];
img = json['img'];
uploader = json['uploader'];
url = json['url'];
createdate = json['createdate'];
approved = json['approved'];
active = json['active'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['id'] = this.id;
data['name'] = this.name;
data['img'] = this.img;
data['uploader'] = this.uploader;
data['url'] = this.url;
data['createdate'] = this.createdate;
data['approved'] = this.approved;
data['active'] = this.active;
return data;
}
}