Flutter 中 Cloud Firestore 的 FieldValue.increment

Hod*_*fca 7 firebase flutter google-cloud-firestore

我正在使用 flutter 和 Firebase Firestore 创建一个 android 应用程序。我想添加新的 FieldValue.increment 功能,该功能自今年 4 月起在 Firebase 中可用,但出现错误: The method 'increment' isn't defined for the class 'FieldValue'.

这是我尝试过的代码:

onTap: (){
  Firestore.instance.document('docRef').updateData({"numberOfDocs": FieldValue.increment(1)});
}
Run Code Online (Sandbox Code Playgroud)

我只是一个初学者程序员,但是当我检查那个 field_value.dart 文件时,它缺少“增量”实现。那么,说 Flutter 团队尚未实现该功能是否正确?

我看过一个教程,其中一些 .js 代码中已经使用了增量,但对我来说它不起作用。 https://www.youtube.com/watch?v=8Ejn1FLRRaw

Soh*_*lam 17

导入的用户:

import 'package:cloud_firestore/cloud_firestore.dart';
Run Code Online (Sandbox Code Playgroud)

对于增量:

await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(1)});
Run Code Online (Sandbox Code Playgroud)

对于减量:

await FirebaseFirestore.instance.collection('post').doc(postId).update({"like": FieldValue.increment(-1)});
Run Code Online (Sandbox Code Playgroud)


cre*_*not 16

这已被添加到cloud_firestore扑插件作为版本0.10.0

您可以使用FieldValue.incrementa double(eg FieldValue.increment(1.0)) 或 an int(eg FieldValue.increment(2))。


这是相关的拉取请求

如果有人在将版本推送到 Pub 之前阅读此内容:该版本的链接尚无法使用,但同时您可以使用此 Git commit,其中包含我所做的修复。


Fra*_*len 8

据我在FlutterFire 的更改日志中看到,尚未添加增量运算符。

向 repo 添加了一个功能请求

  • 这似乎现在在主仓库中:https://github.com/flutter/plugins/pull/1491 (3认同)