颤振和json:NoSuchMethodError:方法'[]'在null上被调用
Posted
技术标签:
【中文标题】颤振和json:NoSuchMethodError:方法\'[]\'在null上被调用【英文标题】:flutter and json : NoSuchMethodError: The method '[]' was called on null颤振和json:NoSuchMethodError:方法'[]'在null上被调用 【发布时间】:2021-01-24 10:25:32 【问题描述】:你好,我正在制作一个新的颤振应用程序,我是颤振新手,我正在使用一个 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
]
每当我尝试实现文件夹和文件列表时,它都会说列表中没有任何内容,并且它为 null 这是我的代码: 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>
List<Folders> list= <Folders>[];
Future<List<Folders>> getData() async
String link = "http://msc-mu.com/api_verfication.php";
try
var res = await http.post(link,
body:
'flag': 'selectfellowsubjects',
'parentsubject': 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');
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 (32601): NoSuchMethodError: The method '[]' was called on null.
I/flutter (32601): Receiver: null
I/flutter (32601): Tried calling: []("id")
这是我的Folders.class
class Folders
String id;
String name;
Folders(this.id, this.name);
factory Folders.fromJson(Map<String, dynamic> json)=> Folders(id: json["folders"]["id"].toString(),name: json["folders"]["name"]);
我做错了吗?
【问题讨论】:
查看哪一行抛出错误总是有帮助的 我怎么能看到? 【参考方案1】:删除['folders']
factory Folders.fromJson(Map<String, dynamic> json)=> Folders(id: json["folders"]["id"].toString(),name: json["folders"]["name"]);
您已经在浏览data["folders"].forEach
中的文件夹并将每个对象传递给构造函数。
因此,没有单个对象具有“文件夹”属性。
"id":2108,
"name":"L1",
"img":null,
"haschild":0,
"parentid":1955,
"createdate":"2019-09-30 03:00:45",
"inarchive":0,
"active":1
,
【讨论】:
我能再问你一件事吗? 我想将此列表的值获取到其他类中我该怎么做,因为您可以看到该函数正在使用 (widget.value.id) 调用其他列表值,但我似乎无法在这个中做同样的事情为什么?这个清单很简单 您是否尝试将列表作为参数传递给其他类?以上是关于颤振和json:NoSuchMethodError:方法'[]'在null上被调用的主要内容,如果未能解决你的问题,请参考以下文章
怎么解决?颤振(6629):[错误:颤振/lib/ui/ui_dart_state.cc(209)]未处理的异常:NoSuchMethodError
“未处理的异常:NoSuchMethodError:在 null 上调用了 getter 'iterator'。”当我在颤振中使用 google_maps_polyline 插件时抛出