如何从 Firebase 存储中检索元数据?

How to retrieve metadata from firebase storage?

info(String choice) async {
    print(choice);
    if (choice == 'Created on') {
      print('hii');
      StorageReference imageRef = storageRef.child(imageLocation);
      await imageRef
          .getMetadata()
          .then((value) => print(value.creationTimeMillis));
      int created = 3;
      String create = timeago.format(
        DateTime.fromMillisecondsSinceEpoch(created),
      );

      print(create);
    }
    Scaffold.of(context).showSnackBar(
      SnackBar(
        content: Text(" hiii"),
      ),
    );
  }

这个方法我用过,但是好像不行 上面的方法是什么

我想你正在寻找这个:

StorageReference imageRef = storageRef.child(imageLocation);
imageRef
    .getMetadata()
    .then((value) => {
        String create = timeago.format(
          DateTime.fromMillisecondsSinceEpoch(value.creationTimeMillis),
        );
        print(create);
    })

所有需要元数据的代码都需要在 then 块中。


或者,如果您想使用 await,则不需要 then():

StorageReference imageRef = storageRef.child(imageLocation);
var value = await imageRef.getMetadata()
String create = timeago.format(
  DateTime.fromMillisecondsSinceEpoch(value.creationTimeMillis),
);
print(create);