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
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.
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')));
});
Where am I going Wrong?