firebase – 使用Flutter将对象添加到Cloud Firestore

firebase – 使用Flutter将对象添加到Cloud Firestore,第1张

概述我想在我的Flutter应用中向Google Cloud Firestore添加一个对象,如下所示: 我已经做了一个回复课: class Reply {Reply(this.replyName, this.replyText, this.replyVotes); final String replyName; final String replyText; final String 我想在我的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对象?

编辑:
为了澄清,我想创建一个数据类型为Object的字段,其中包含字段:Reply Object Image

解决方法 首先,我强烈建议您使用单个文件来定义所有模式和/或模型,这样您的数据库结构就有一个参考点.像一些名为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());  ....
总结

以上是内存溢出为你收集整理的firebase – 使用Flutter将对象添加到Cloud Firestore全部内容,希望文章能够帮你解决firebase – 使用Flutter将对象添加到Cloud Firestore所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://www.54852.com/web/1001884.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-22
下一篇2022-05-22

发表评论

登录后才能评论

评论列表(0条)

    保存