Firebase runTransaction() 在Dart中的使用

huangapple go评论70阅读模式
英文:

Firebase runTransaction() Typing in Dart

问题

我正在寻求在Dart中处理Firebase实时数据库事务的输入帮助。

具体来说,我正在尝试处理来自Firebase的示例事务代码

  void transactions_updatingDataWithTransactions() {
    // [START transactions_updating_data_with_transactions]
    final sfDocRef = db.collection("cities").doc("SF");
    db.runTransaction((transactionObj) async {
      final snapshot = await transactionObj.get(sfDocRef);
      final newPopulation = snapshot.get("population") + 1;
      transactionObj.update(sfDocRef, {"population": newPopulation});
    }).then(
      (value) => print("DocumentSnapshot successfully updated!"),
      onError: (e) => print("Error updating document $e"),
    );
  }

问题出在db.runTransaction()的参数是Transaction Function(Object?) transactionHandler,其中在上面的代码中,transactionObj 是该对象。当尝试执行任何操作,比如:

      final snapshot = await transactionObj.get(sfDocRef);

我会收到关于“get”未定义于Object?的错误。我该如何解决这个类型问题?

实时数据库文档中没有Flutter代码,可以参考这里

我基于Firestore文档进行操作,可以参考这里,因为我无法找到与实时数据库相关的文档。

英文:

I am seeking help with typing of a Firebase Realtime Database transaction within dart.

Specifically I am trying to work through the example transaction code from firebase

  void transactions_updatingDataWithTransactions() {
    // [START transactions_updating_data_with_transactions]
    final sfDocRef = db.collection("cities").doc("SF");
    db.runTransaction((transactionObj) async {
      final snapshot = await transactionObj.get(sfDocRef);
      final newPopulation = snapshot.get("population") + 1;
      transactionObj.update(sfDocRef, {"population": newPopulation});
    }).then(
      (value) => print("DocumentSnapshot successfully updated!"),
      onError: (e) => print("Error updating document $e"),
    );
  }

The issue arises from the argument of db.runTransaction() is Transaction Function(Object?) transactionHandler

Which in the above, transactionObj is the object. When trying to perform any operations such as

      final snapshot = await transactionObj.get(sfDocRef);

I get errors that "get" isn't defined for Object?. How do I get around this typing?

The RTDB documentation does not have flutter code see here

And the Firestore documentation is the one I am basing this off of see here because I cannot find any documentation related to the Realtime Database

答案1

得分: 0

Here is the translated content:

如果你查看Transaction的参考文档,它确实有一个get()方法。但错误消息表明编译器认为transactionObj的类型是Object?,这意味着它不知道它的方法。

你可以通过在你的代码中指定transactionObj的类型来修复这个问题。如果我记得正确,应该是这样的:

db.runTransaction((transactionObj: Transaction) async {

或者,你可以在回调中将transactionObj转换为正确的类型:

db.runTransaction((transactionObj) async {
  final transaction = transactionObj as Transaction;

你提到的代码(例如db.collection("cities").doc("SF");)适用于Cloud Firestore。如果你没有使用Firestore,那么你似乎正在尝试使用错误的API。

要了解实时数据库的事务API,请参阅:https://firebase.google.com/docs/database/flutter/read-and-write#save_data_as_transactions

英文:

If you look at the reference documentation for Transaction, is does have a get() method. But the error message indicates that the compiler thinks that transactionObj is of type Object?, and that means it doesn't know about its methods.

You can fix this by specifying the type of transactionObj in your code. If I recall correctly that should be something like:

db.runTransaction((transactionObj: Transaction) async {

Alternatively, you can cast transactionObj to the correct type in your callback with:

db.runTransaction((transactionObj) async {
  final transaction = transactionObj as Transaction;

The code in your question (e.g. db.collection("cities").doc("SF");) is for Cloud Firestore. If you're not using Firestore, it sounds like you're trying to use the wrong API.

To learn about the transaction API for the Realtime Database, see: https://firebase.google.com/docs/database/flutter/read-and-write#save_data_as_transactions

huangapple
  • 本文由 发表于 2023年6月12日 02:05:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76451860.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定