颤振:将 json 实现为列表返回一个空列表

Posted

技术标签:

【中文标题】颤振:将 json 实现为列表返回一个空列表【英文标题】:flutter : implementing json as list returns a null list 【发布时间】:2021-01-24 03:13:13 【问题描述】:

你好,我正在制作一个新的颤振应用程序,我是颤振新手,我使用的是 mysql 数据库,我得到的响应是 json 。 这是我格式化的 json 响应:

JsonResponse


   "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
      
   ]

每当我尝试实现文件夹和文件列表时,它都会说列表中没有任何内容,并且它为空 这是我的代码: fellow.dart


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();
          ,
        ),

      ),
    );
  

这是输出

I/flutter (21630): "folders":[],"files":[]
I/flutter (21630): []
I/flutter (21630): List Size: 0

有什么帮助吗?

【问题讨论】:

嗨!你可以试试 data["folders"].forEach((v) list .add(Folders.fromJson(v)); ); ` Future> getData() async List list;字符串链接 = "msc-mu.com/api_verfication.php"; var res = await http.post(link,body: '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)); );打印(休息); list = rest.map((json) => Folders.fromJson(json)).toList(); 返回列表; ` 如果你的意思是这样,它不起作用,同样的事情返回列表为空 删除行列表 = rest.map((json) => Folders.fromJson(json)).toList(); 你能写出完整的函数代码吗? 【参考方案1】:
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');
    
  

【讨论】:

type 不是 type 的子类型 你可以再试一次,告诉我更多细节! 我们可以联系discord吗 我把它作为答案把我需要帮助请... 对不起!这是我的错,您只需将 debugPrint(e) 更改为 debugPrint('$e')。【参考方案2】:
 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;
  

在制作完这样的模型后,你应该做空检查来检查每个值是否有一个值。

【讨论】:

你能告诉我下一步该怎么做吗?或更新您的答案以在 ui 及其功能中实现它? 您现在可以通过创建模型的实例来访问模型。模型制作完成后,我建议您调用模型并对其进行空检查。

以上是关于颤振:将 json 实现为列表返回一个空列表的主要内容,如果未能解决你的问题,请参考以下文章

Flutter / Dart / Firebase - 流返回空列表

使用 api 在颤振中返回卡片列表

颤振传递列表到另一个类总是空的

Flutter 函数使用 Firebase 返回空列表

ListView OnClicklistener 仅返回单个 json 项值

函数返回空列表而不是 List<String> [重复]