Home > Blockchain >  Flutter & Dart : how to retrive map field data from firestore?
Flutter & Dart : how to retrive map field data from firestore?

Time:06-16

enter image description here

Maps key is dynamic key, so i cant set a class to use fromjson method,

Map<String, double> rating;

i set the data , it working;

data['rating'] = json.encode(this.rating);

i try to get data, not working;

rating = jsonDecode(json['rating']);

igot the error:

Expected a value of type 'Map<String, double>', but got one of type 'String'

how can i get the data as Map ?

CodePudding user response:

json.encode turns it into a string. I believe you actually want to just do

data['rating'] = this.rating;

instead of

data['rating'] = json.encode(this.rating);

CodePudding user response:

here is the solution;

i used this;

data['rating'] = FieldValue.arrayUnion([this.rating]);  

instead of

data['rating'] = json.encode(this.rating); 

or

data['rating'] = this.rating; //both not working

for getting data; (I don't like this solution, but worked)

var asd = json["rating"] as List;
Map qwee = asd[0];
String rtkey = qwee.keys.toList()[0].toString();
double rtvalue = qwee.values.toList()[0];
rating = {rtkey: rtvalue} ;
  • Related