Flutter:方法[]在null上被调用 [英] Flutter: The method [] was called on null
问题描述
我知道这个问题被问过很多次,但是我的问题有点不同.我正在扑朔迷离地使用Firebase实时数据库.我在数据库中上载了我现有数据的JSON文件.看起来像是此
I know this question is asked and answered many times but my problem is a bit different. I am working with firebase realtime database in flutter. I upload a JSON file in the database for the existing data I have. It looks like this
每个孩子都有4个属性(名称,位置,质量,大小)子代的ID为0,1,2 .....当我在应用程序中检索此数据库时,它可以正常工作,并且显示完美.
Each child has 4 properties (Name, Location, Quality, Size) The children have IDs of 0,1,2..... When I retrieve this database in my app, it works and it is displayed perfectly.
当我通过我的应用创建新条目时,孩子有一个随机ID,看起来像此.
When I create a new entry from my app, the child has a random ID and it looks like this.
之后,当我尝试检索值时,这些值会打印在控制台中,但在屏幕上会显示:-
After that when I try to retrieve the values, the values are printed in the console but on the screen I get:-
The method [] was called on null error. Receiver: null. Tried Calling: []("Name")
. The Error screen looks like this. Error in console looks like this
我的检索代码(我将变量获取并将其传递到另一个屏幕):-
My code for retrieving (I fetch and pass the variable to another screen):-
ref.once().then((DataSnapshot data) {
datatosend = data;
print(datatosend.value);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DisplayList(
text: datatosend,
title: CategoryItemsitems[index].name),
));
});
我用于显示列表视图的代码:
My code for displaying listview:
itemCount: widget.text.value.length,
itemBuilder: (BuildContext context, int index) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: ListTile(
leading: IconButton(
icon: Icon(Icons.edit),
onPressed: () {
print("Edit Pressed!");
}),
title: Text(widget.text.value[index]["Name"]),
subtitle: Text("Quality: " +
widget.text.value[index]["Quality"] +
"\nSize: " +
widget.text.value[index]["Size"] +
"\nLocation: " +
widget.text.value[index]["Location"]),
trailing: IconButton(
icon: Icon(
Icons.delete,
color: Colors.red,
),
onPressed: () {
print("Delete Pressed!");
}),
),
);
}),
我用于创建新条目的代码:
My code for creating a new entry:
databaseReference.child(_category).push().set({
"Name": _name,
"Quality": _quality,
"Size": _size,
"Location": _location
}).then((_) {
Scaffold.of(context).showSnackBar(
SnackBar(content: Text('Successfully Added')));
});
我要去哪里错了?
推荐答案
大约10天后,我的问题终于解决了.我做了一些更改:
After about 10 days, my problem is finally solved. Some changes I made:
- 我正在使用Cloud Firestore,而不是使用实时数据库.
- 代替导入JSON文件,我将手动输入数据.幸运的是,它不是很大,而且之前我也只是手动编写JSON文件.
将数据添加到Firestore的代码.所有这些都在压制的"内部.按钮的参数.所有输入均采用简单的形式.
Code to add the data to Firestore. All of this is inside the "onpressed" parameter of a button. All the inputs are taken by a simple form.
firestoreInstance.collection("cars").add({
"Name": _name,
"Quality": _quality,
"Size": _size,
"Location": _location,
}).then((value) {
print("Added Successfully")});
为了获取数据并访问其不同字段,我使用了流构建器:
For fetching the data and access its different fields I am using a stream-builder:
StreamBuilder(
stream: Firestore.instance
.collection("cars")
.orderBy("Name") //To display the list by Name
.snapshots(),
builder: (context, snapshot) {
if (!snapshot.hasData) {
return Center(child: CircularProgressIndicator());
}
else {
if(snapshot.data.documents.length>0)
{
return ListView.builder(
itemCount: snapshot.data.documents.length, //no. of entries fetched
itemBuilder: (BuildContext context, int index) {
return ListTile(
title: Text(
snapshot.data.documents[index].data()["Name"]), //Accessing the Name property
subtitle: Text("Quality: " +snapshot.data.documents[index].data()["Quality"] + //Accessing the Quality property
"\nSize: " +snapshot.data.documents[index].data()["Size"] + //Accessing the Size property
"\nLocation: " +snapshot.data.documents[index].data()["Location"]) //Accessing the Location property
);
}
)}
}
}
}
)
如果某些右括号不匹配,我深表歉意.我从非常混乱的代码中提取了这些重要的片段.
I apologize if some of the closing parenthesis does not match. I pulled these important snippets from my very messy code.
这篇关于Flutter:方法[]在null上被调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!