如何使用Flutter在FireStore中的集合中创建文档

集合在Firebase控制台上具有默认权限。

我使用电子邮件和密码正确登录了用户。

user = await _auth.signInWithEmailAndPassword( email: "[email protected]", password: "password");

然后,在将图像成功上传到FireStorage之后,我也尝试运行事务以更新文档。

  var fileName = _textController.text.toLowerCase();
  StorageUploadTask putFile =
      storage.ref().child("region/$fileName").putFile(_regionImage);

  UploadTaskSnapshot uploadSnapshot = await putFile.future;

  var regionData = new Map();
  regionData["label"] = _textController.text;
  var pictureData = new Map();
  pictureData["url"] = uploadSnapshot.downloadUrl.toString();

  pictureData["storage"] = "gs://app-db.appspot.com/region/$fileName";
  regionData["picture"] = pictureData;

  DocumentReference currentRegion =
      Firestore.instance.collection("region").document(fileName);

  Firestore.instance.runTransaction((transaction) async {
    DocumentSnapshot freshSnap = await transaction.get(currentRegion);
    print(freshSnap.exists);
    //await transaction.set(freshSnap.reference, regionData);
    await transaction.set(currentRegion, regionData);
    print("instance created");
  });

尝试运行事务时收到此错误。

如果我尝试设置为freshSnap.reference或直接设置为,则相同currentRegionhttps://gist.github.com/matejthetree/f2a57c929d01919bd46da8ca6d5b6fb1

请注意,在第15行,交易开始时出现错误,但在我成功上载映像并将其下载到存储设备之前,FireStorage也没有收到身份验证令牌错误。

我应该如何在FireStore中进行文档创建

好像问题出在我创建地图的方式上。

  Map<String, dynamic> regionData = new Map<String, dynamic>();
  regionData["label"] = _textController.text;
  Map<String, dynamic> pictureData = new Map<String, dynamic>();
  pictureData["url"] = uploadSnapshot.downloadUrl.toString();

  pictureData["storage"] = "gs://app-db.appspot.com/region/$fileName";
  regionData["picture"] = pictureData;

  DocumentReference currentRegion =
      Firestore.instance.collection("region").document(fileName);

  Firestore.instance.runTransaction((transaction) async {
    await transaction.set(currentRegion, regionData);
    print("instance created");
  });

该代码现在有效

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

如何使用Flutter在Firestore中的uid文档中创建集合?

使用 Flutter 在 Cloud Firestore 中创建文档

如何使用Flutter从Firestore中的集合中获取特定文档

使用 Flutter 创建用户集合后,如何在 Firestore 中检索文档 ID

如何在Firestore中创建文档字段=映射?

使用云功能在Firestore中创建文档

使用 Apple 登录时无法在 Firestore 中创建文档

如何在使用颤振在 Firestore 中创建的用户文档中添加子集合

在 Firestore 中创建文档时无法指定文档 ID

如何使用firebase firestore flutter的自动生成的ID删除集合中的特定文档?

如何使用表单验证通过Flutter验证集合Firestore中是否存在文档

如何从 Flutter 和 Firestore 中“Y”集合的文档 id 查询“X”集合的文档

如果文档不存在,如何根据 firestore 中的 timsteamp 创建文档?

如果Firestore中不存在文档,如何添加到数组并创建文档?

从firestore的文档查询集合中创建列表

如何在MongoDB中创建文档?

如何使用子集合创建文档

在 Cosmos DB 集合中手动创建文档

如何使用集合更新 firebase firestore 文档中的字段

如何在Gradle中创建文件集合的集合?

如何获取firestore集合中的文档数

Flutter Firestore:集合中的特定文档 (Firestore.collection.where)

如何使用flutter在Firestore中的文档中添加地图?

如何检查集合和文档是否存在,如果不存在,则使用 .net 在 FireStore 中创建具有特定 ID 的集合和文档

Firebase 函数 - 在子集合中创建文档时从主集合中获取数据

在Elasticsearch中创建文档

如何根据 dart/flutter 中的日期从 firebase/firestore 集合中检索 1 个文档?

如何从 firestore 中获取文档集合并将其转换为 flutter 中的列表?

在Firestore中的子集合中创建多文档