Flutter Firestore交易无法捕获错误?

时间:2019-11-19 21:56:37

标签: flutter google-cloud-firestore

以下内容将捕获错误并显示“用户已锁定”。如果我从该部分抛出,我的应用程序将崩溃。如何获取错误并一直传递到onPressed?

Future activate(String uid) {
return _fire.runTransaction((transaction) {
  var ref = buildUser(uid);
  return transaction.get(ref).then((userDoc) {
    var user = Profile.fromJson(userDoc.data);

    if (user.isLocked) throw "User is locked";

    ....

    return transaction.update(ref, user.toJson());
  }).catchError((onError) {
      print(onError);
  });
});

它称为onPressed

try {
  await api.activate(uid);
} catch (e) {
  print('onAction $e');
}

0 个答案:

没有答案