gpt4 book ai didi

dart - Firebase(Dart) future 异常(exception)

转载 作者:行者123 更新时间:2023-12-03 03:27:46 29 4
gpt4 key购买 nike

我正在尝试使用Firebase软件包,看是否可以在我的应用程序中使用它。 https://pub.dartlang.org/packages/firebase

我正在尝试用Javascript隐藏此代码

var upvotesRef = new Firebase('https://docs-examples.firebaseio.com/android/saving-data/fireblog/posts/-JRHTHaIs-jNPLXOQivY/upvotes');
upvotesRef.transaction(function (current_value) {
return (current_value || 0) + 1;
});

取自 https://www.firebase.com/docs/web/guide/saving-data.html#section-transactions

我在 Dart 上的尝试是:

int getMrn() {
var testRef = firebaseClient.child('mrn');
return testRef.transaction((curVal) {
return curVal == null ? 1 : curVal + 1;
}).then((result) {

var snapshot = result.snapshot;
print( snapshot.val());

});
}

返回正确的curVal,并将mrn正确写入数据库。还会打印正确的值。

但是,出现以下错误,我不确定原因:
Exception: Uncaught Error: type '_Future' is not a subtype of type 'int' of 'function result'.
Stack Trace:
#0 getMrn (package:epimss_db/firebase_db.dart:26:15)
#1 onMenuItemSelected (package:epimss_lab/components/chempath/nephrology/electrolytes_rqst_form.dart:116:17)
#2 Function.apply (dart:core-patch/function_patch.dart:28)
#3 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#4 invoke (package:smoke/smoke.dart:43:41)
#5 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#6 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#7 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#8 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#9 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)
#10 BlinkEventTarget.dispatchEvent_Callback_1 (dart:_blink:7764)
#11 BlinkEventTarget.dispatchEvent_Callback_1_ (dart:_blink:7765)
#12 EventTarget.dispatchEvent (dart:html:14741)
#13 onMainActionClicked (package:html_components/menu/split_button.dart:34:23)
#14 Function.apply (dart:core-patch/function_patch.dart:28)
#15 GeneratedObjectAccessorService.invoke (package:smoke/static.dart:149:28)
#16 invoke (package:smoke/smoke.dart:43:41)
#17 HtmlElement&Polymer.dispatchMethod (package:polymer/src/instance.dart:1054:19)
#18 BindingDelegate&PolymerEventBindings.getEventHandler.<anonymous closure> (package:polymer/src/events.dart:82:32)
#19 _RootZone.runUnaryGuarded (dart:async/zone.dart:1093)
#20 _RootZone.bindUnaryCallback.<anonymous closure> (dart:async/zone.dart:1122)
#21 BindingDelegate&PolymerEventBindings.prepareEventBinding.<anonymous closure>.<anonymous closure> (package:polymer/src/events.dart:101:67)

1

谢谢

最佳答案

Future<int> getMrn() { // <== 3
var testRef = firebaseClient.child('mrn');
return testRef.transaction((curVal) { // <== 2)
return curVal == null ? 1 : curVal + 1;
}).then((result) {
var snapshot = result.snapshot;
print( snapshot.val());
return result; // <== 1)
});
}

1)从最后链接的 then()返回的值在2)返回
3)因为这是异步代码,所以返回 Future<int>而不是 int

您将这样的代码称为

getMrn().then((result) {
// doSomething with the code here
});

或等待

void someFunc() async { // <== async necessary in order to be able to use await
var result = await getMrn();
}

关于dart - Firebase(Dart) future 异常(exception),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27371542/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com