简体   繁体   中英

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM