I am trying to save my Poll Object to Firebase Realtime DB, but I dont really know how to do that with my Poll Object because it has a List. I tried to find a tutorial how to map an object with map to firebase but didnt find any.
class Poll {
String id;
String name;
String description;
List<Question> questions;
Poll({this.name, this.description, this.questions});
Poll.fromSnapshot(DataSnapshot snapshot) {
id = snapshot.key;
name = snapshot.value['name'];
description = snapshot.value['description'];
questions = snapshot.value['questions'];
}
toJson() {
return {'name': name, 'description': description, 'questions': questions};
}
}
class Question {
String id;
String question;
String customAnswer;
Question.customAnswer({this.question, this.customAnswer});
Question.fromSnapshot(DataSnapshot snapshot) {
id = snapshot.key;
question = snapshot.value['question'];
customAnswer = snapshot.value['customAnswer'];
}
toJson() {
return {'question': question, 'customAnswer': customAnswer};
}
}
Here I try to write to DB:
RaisedButton(
onPressed: () async {
Poll poll1 =
Poll(name: 'poll1', description: 'desc1', questions: [
Question.customAnswer(
question: 'who am i', customAnswer: 'Ostap'),
Question.customAnswer(
question: 'who are you', customAnswer: 'test'),
]);
await databaseReference
.child('Polls')
.push()
.set(poll1.toJson());
},
child: Text('Write To DB'),
And here the error Im getting:
Exception has occurred.
ArgumentError (Invalid argument: Instance of 'Question')
Its caused on await databaseReference
Can somebody help me? Thanks in advance!