I use a json file in a flutter app and recording this json in firestore fails.
my json is in jsonCloudContent. The following doesn't work :
Firestore.instance.collection('toto').document().setData(jsonCloudContent);
When I try with the string from json.encode(jsonCloudContent) it works fine :
Firestore.instance.collection('userStuffLists').document().setData({"apiVersion":2,"userId":"unknown","googleId":"unknown","list":[{"userI...
here's an example of the json file : https://jsoneditoronline.org/?id=3ac25ef1743047e08467cccbad031d5e
The function to upload data :
Future<bool> uploadUserStuffList() async {
Map<String, dynamic> jsonCloudContent = StuffList(
list: list,
apiVersion: apiVersion,
userId: userId,
googleId: googleId,
listVersion: listVersion)
.toJson();
try {
Firestore.instance.collection('toto').document().setData(jsonCloudContent);
} catch (e) {
debugPrint(e);
}
return true;
}
In Intellij inspector the format looks fine but just in case, here's the objects definition :
class Stuff {
final String description;
final List<String> imagePath;
final String userId;
// TODO : put unknown in the key_strings ?
Stuff({this.userId = 'unknown', this.description, this.imagePath});
factory Stuff.fromJson(Map<String, dynamic> json) {
return new Stuff(
userId: json['userId'] != null ? json['userId'] : 'unknown',
// TODO : throw error if description is null ? not certain
description: json['description'],
imagePath: json['path'] != null ? List<String>.from(json['path']) : null,
);
}
Map<String, dynamic> toJson() => {
'userId': userId != null ? userId : 'unknown',
'description': description,
'path': imagePath,
};
}
class StuffList {
final List<Stuff> list;
final int apiVersion;
// TODO need appropriate format when integrate firebase and google authentication
final String userId;
final String googleId;
final int listVersion;
StuffList({
this.apiVersion = 2,
this.userId = 'unknown',
this.googleId,
this.listVersion = 1,
this.list,
});
factory StuffList.fromJson(Map<String, dynamic> json) {
List<Stuff> listOfStuff = [];
if (json['list'] != null) {
json['list'].forEach((content) {
Stuff stuff = Stuff.fromJson(content);
listOfStuff.add(stuff);
});
} else {
listOfStuff = null;
}
return new StuffList(
apiVersion: json['apiVersion'],
userId: json['userId'] != null ? json['userId'] : 'unknown',
googleId: json['googleId'] != null ? json['googleId'] : 'unknown',
listVersion: json['listVersion'],
// list: json['list'] != null ? (json['list'] as List).map((i) => new Stuff.fromJson(i)) : null,
list: listOfStuff,
);
}
Map<String, dynamic> toJson() => {
'apiVersion': apiVersion != null ? apiVersion : 1,
'userId': userId != null ? userId : 'unknown',
'googleId': googleId != null ? googleId : 'unknown',
'listVersion': listVersion != null ? listVersion : 1,
'list': list,
};
}
Found my error. The object StuffList contains a list of Stuff objects. The toJson method didn't convert list of Stuff to json. Here's the new method :
Map<String, dynamic> toJson() => {
'apiVersion': apiVersion != null ? apiVersion : 1,
'userId': userId != null ? userId : 'unknown',
'googleId': googleId != null ? googleId : 'unknown',
'listVersion': listVersion != null ? listVersion : 1,
'list': list.map((i) => i.toJson()).toList(),
};
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With