gpt4 book ai didi

firebase - 如何在 flutter 应用程序的 Cloud firestore 中记录带有嵌套数组的 json

转载 作者:IT王子 更新时间:2023-10-29 07:22:55 29 4
gpt4 key购买 nike

我在 flutter 应用程序中使用了一个 json 文件,但在 firestore 中记录这个 json 失败了。

我的 json 在 jsonCloudContent 中。以下不起作用:

Firestore.instance.collection('toto').document().setData(jsonCloudContent);

当我尝试使用 json.encode(jsonCloudContent) 中的字符串时,它工作正常:

Firestore.instance.collection('userStuffLists').document().setData({"apiVersion":2,"userId":"unknown","googleId":"unknown","list":[{"userI...

这是一个 json 文件的例子:https://jsoneditoronline.org/?id=3ac25ef1743047e08467cccbad031d5e

上传数据的函数:

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;
}

在 Intellij inspector 中,格式看起来不错,但为了以防万一,这里是对象定义:

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,
};
}

最佳答案

发现我的错误。对象 StuffList 包含 Stuff 对象的列表。toJson 方法没有将 Stuff 列表转换为 json。这是新方法:

  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(),
};

关于firebase - 如何在 flutter 应用程序的 Cloud firestore 中记录带有嵌套数组的 json,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54706708/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com