EddyLee
EddyLee

Reputation: 803

Flutter save array to Firebase Database

I am making a small example with Flutter, the data is saved on Firebase Database Realtime as an array, on the client side I will download the array, display it on a list view then customize it, end the process I want to save directly that array on Firebase. But when I use the following code

myRef.set(myArray);

And error:

Unhandled Exception: Invalid argument: Instance of...

I tried converting my array to json array by the following code

String jsonArray = jsonEncode(myArray);
myRef.set(jsonArray);

then the data was set successfully, but the result was a string that appeared on Firebase, not a list I wanted.

So I can ask questions here, expect a correct answer from everyone, very respectfully

Upvotes: 0

Views: 2041

Answers (1)

EddyLee
EddyLee

Reputation: 803

Okay i got an answer

I just create an Map<String,dynamic> to repersent for each item in myArray and put it to List like that

List<Map<String,dynamic>> result = new List<Map<String,dynamic>>();
        myArray.forEach((item) {
          result.add(item.toJson());
        });

And final, i just set 'result' for Firebase

stepRef.set(result);

Thank you for reading :D

Upvotes: 2

Related Questions