所以我在 firestore 中有一个名为 members 的数组,到目前为止它所做的只是保存已加入该列表的用户显示名称的列表。但是,我需要的是能够在此数组中创建一个 map ,该 map 将保存每个成员的两个唯一值、他们的名字和他们收集的塑料数量。我对如何实现这一点感到很困惑。
我认为 map 结构应该是这样的,但我可能错了:
class MemberList {
final String memberName;
final int plastics;
MemberList(this.memberName, this.plastics);
Map<String, dynamic> toMap() =>
{"memberName": this.memberName, "plastics": this.plastics};
}
我正在尝试将此数组中的值保存在我的其他模型中:
class Group {
String id;
String name;
String admin;
List<String> members;
Group({this.id, this.name, this.admin, this.members});
}
在我的数据库中,我有这个功能可以让用户加入组,在这里我可以创建我的数组。首先,我创建了一个名为 displayName 的字符串:
Future<String> joinGroup(
String groupId, String userUid, String displayName) async {
String retVal = 'error';
List<String> members = List();
try {
members.add(displayName);
await _firestore.collection('Groups').doc(groupId).update({
'members': FieldValue.arrayUnion(members),
});
final uid = FirebaseAuth.instance.currentUser.uid;
await _firestore.collection('UserNames').doc(uid).update({
'groupId': groupId,
});
retVal = 'success';
} catch (e) {}
return retVal;
}
当用户真正加入时,这个函数被调用,它获取他们的 displayName 并将其添加到成员数组中:
void _joinGroup(BuildContext context, String groupId) async {
String uid = auth.currentUser.uid.toString();
final CollectionReference users =
FirebaseFirestore.instance.collection('UserNames');
final name = await users.doc(uid).get();
final result = name.data()['displayName'];
String _returnString =
await GroupDatabase().joinGroup(groupId, uid, result);
if (_returnString == 'success') {
Navigator.of(context)
.pushAndRemoveUntil(groupPageView.route, (route) => false);
}
}
从视觉的角度来看,这就是我的 firestore 文档现在存储数组的方式:
任何帮助将不胜感激,因为我在 map 方面遇到了困难。谢谢。
最佳答案
按照这个模型直接获取Group类就可以了。您不需要特别指定 MemberList 模型。 例如;
await _firestore.collection('Groups').then(value){
Map<String, dynamic> data = json.decode(value);
List<dynamic> result = data['groups'];
List<Groups > groups =
result.map((f) => Groups .fromJson(f)).toList();
}
我可能输入了错误的 firebase 调用方法。但模型必须是这样的。
import 'dart:convert';
Groups cargoPriceListModelFromJson(String str) =>
Groups.fromJson(json.decode(str));
String cargoPriceListModelToJson(Groups data) => json.encode(data.toJson());
class Groups {
String id;
String name;
String admin;
List<Member> members;
Groups({
this.id,
this.name,
this.admin,
this.members,
});
factory Groups.fromJson(Map<String, dynamic> json) => Groups(
id: json["id"] == null ? null : json["id"],
name: json["name"] == null ? null : json["name"],
admin: json["admin"] == null ? null : json["admin"],
members: json["members"] == null
? null
: List<Member>.from(json["members"]
.map((x) => Member.fromJson(x))),
);
Map<String, dynamic> toJson() => {
"id": id == null ? null : id,
"name": name == null ? null : name,
"toCountyId": admin == null ? null : admin,
"members": members == null
? null
: List<dynamic>.from(members.map((x) => x.toJson())),
};
}
class Member {
String memberName;
int plastics;
Member({
this.memberName,
this.plastics,
});
factory Member.fromJson(Map<String, dynamic> json) =>
Member(
memberName: json["memberName"] == null ? null : json["memberName"],
plastics: json["plastics"] == null ? null : json["plastics"],
) ;
Map<String, dynamic> toJson() => {
"memberName": memberName == null ? null : memberName,
"plastics": plastics == null ? null : plastics,
};
}
关于firebase - 如何在数组 flutter Firestore 中添加映射,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65177415/