【问题标题】:Dart return Future.value is always nullDart 返回 Future.value 始终为 null
【发布时间】:2021-09-13 10:40:35
【问题描述】:

我正在尝试从 Firebase 存储文件构建 URL,但我构建的 Future<String> 似乎总是返回 null。这是Future我打电话:

Future<String> getUrlFromStorageRefFromDocumentRef(
      DocumentReference docRef) async {
    try {
      docRef.get().then((DocumentSnapshot documentSnapshot) {
        if (documentSnapshot.exists) {
          String filename = documentSnapshot.get('file');
          firebase_storage.Reference ref = firebase_storage
              .FirebaseStorage.instance
              .ref()
              .child('/flamelink/media/$filename');
          if (ref == null) {
            return Future.error("Storage Reference is null");
          } else {
            print(ref.fullPath);
            return Future.value(
                'https://storage.googleapis.com/xxxxxxxxx.appspot.com/${ref.fullPath}');
          }
        } else {
          return Future.error('No Snapshot for DocumentReference ${docRef.id}');
        }
      });
    } catch (e) {
      print(e);
      return Future.error('No DocumentReference for ID ${docRef.id}');
    }
  }

有问题的行是:

return Future.value(
                'https://storage.googleapis.com/xxxxxxxxx.appspot.com/${ref.fullPath}');

值得注意的是,String 是从 Firebase Storage 路径生成的,在返回值之前一切看起来都很完美。

它应该将String 值返回给我的调用代码,目前看起来像这样:

DocButtonCallback docCallback = () async {
      bool isKidsDoc = item.screenId == StringsManager.instance.screenIdKids;
      try {
        // first we need to get the URL for the document ...
        var url = await AssetManager.instance
            .getUrlFromStorageRefFromDocumentRef(isKidsDoc
                ? feature.relatedDocumentKidsRef
                : feature.relatedDocumentRef);

        String urlString = url.toString();

        canLaunch(urlString).then((value) {
          launch(urlString);
        }).catchError((error) {
          // TODO: open alert to tell user
        });
      } catch (error) {
        print(error);
      }
    };

我尝试了许多不同的方法来获得 String,包括:

DocButtonCallback docCallback = () async {
      bool isKidsDoc = item.screenId == StringsManager.instance.screenIdKids;
      await AssetManager.instance
          .getUrlFromStorageRefFromDocumentRef(isKidsDoc
              ? feature.relatedDocumentKidsRef
              : feature.relatedDocumentRef)
          .then((urlString) {
        canLaunch(urlString).then((value) {
          launch(urlString);
        }).catchError((error) {
          // TODO: open alert to tell user
        });
      }).catchError((error) {
        // TODO: open alert to tell user
      });
    };

由于某种原因,Future 总是返回 null。我在这里做错了什么?

【问题讨论】:

    标签: flutter dart async-await dart-async


    【解决方案1】:

    您在then() 回调中返回Future 值,该值实质上是从回调本身而不是从您的getUrlFromStorageRefFromDocumentRef() 函数返回此值。在那里你应该只需要在此之前添加一个 return 语句:

    当前:

    docRef.get().then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        ...
    

    之后:

    /// Adding the return statement here to return the actual value
    /// returned internally by the then callback
    return docRef.get().then((DocumentSnapshot documentSnapshot) {
      if (documentSnapshot.exists) {
        ...
    

    如果您将鼠标悬停在 then() 回调上,您的 IDE 应显示此回调将返回 Future&lt;T&gt;(或任何通用类型占位符),为了使其可用,还需要返回该回调

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-05-31
      • 2015-08-15
      • 2012-03-18
      • 2016-11-04
      • 2016-07-28
      • 2010-11-08
      • 2017-11-26
      相关资源
      最近更新 更多