Reputation: 239
I want to save my List with Objects in Firebase Realtime DB. The Problem is, that when I save my objects there, they dont have an auto generated key, their key is 0, 1, 2,... :
I am mapping my Poll object this way:
Map<String, dynamic> toJson() => {
'id': id,
'name': name,
'allowBarChart': allowBarChart,
'allowPieChart': allowPieChart,
'description': description,
'custom-questions': customQuestions != null
? List<dynamic>.from(customQuestions.map((x) => x.toJson()))
: [],
'choice-questions': choiceQuestions != null
? List<dynamic>.from(choiceQuestions.map((x) => x.toJson()))
: [],
};
My Question is: how to do it, so the key of each value is not 0,1,2,3,... It should be auto generated from firebase, for example: 1fdDfasLPO3g. I hope you know what I mean. Thanks in advance!
Upvotes: 0
Views: 229
Reputation: 598765
When the Firebase SDK encounters a List
it is mapped to a JSON array, which is stored with sequential numeric indices, as seen in the screenshot you shared. There is no way to change this mapping.
If you want to store items with Firebase's push keys (which start with -M
at the moment), you should generate those in your application code (by calling push()
), and store the questions in a Map<string, dynamic>
.
Upvotes: 1