firebase – 使用Flutter将对象添加到Cloud Firestore
发布时间:2020-12-14 14:52:01 所属栏目:百科 来源:网络整理
导读:我想在我的Flutter应用中向Google Cloud Firestore添加一个对象,如下所示: 我已经做了一个回复课: class Reply {Reply(this.replyName,this.replyText,this.replyVotes); final String replyName; final String replyText; final String replyVotes; Strin
我想在我的Flutter应用中向Google Cloud Firestore添加一个对象,如下所示:
我已经做了一个回复课: class Reply { Reply(this.replyName,this.replyText,this.replyVotes); final String replyName; final String replyText; final String replyVotes; String getName() { return replyName; } String getText() { return replyText; } String getVotes() { return replyVotes; } } 如何向云Firestore添加Reply对象? 编辑: 解决方法
首先,我强烈建议您使用单个文件来定义所有模式和/或模型,这样您的数据库结构就有一个参考点.像一些名为dbSchema.dart的文件:
import 'package:meta/meta.dart'; class Replies { final String title; final Map coordinates; Replies({ @required this.title,@required this.coordinates,}); Map<String,dynamic> toJson() => { 'title': title,'coordinates': coordinates,}; } 并使您想要成为对象类型Map的字段.然后,在您要插入数据库的页面上,导入dbSchema.dart并创建一个新模型: Replies _replyObj = new Replies( title: _topic,coordinates: _coordinates,); 这假设你在此之前定义了你的本地_coordinates(或其他)对象,例如: _coordinates = { 'lat': '40.0000','lng': '110.000',}; 然后插入到Firestore中,添加对象的toJson方法(不能插入/更新普通的Dart模型): CollectionReference dbReplies = Firestore.instance.collection('replies'); Firestore.instance.runTransaction((Transaction tx) async { var _result = await dbReplies.add(_replyObj.toJson()); .... (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |