How to retrieve metadata from firebase storage?

Issue

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"),
      ),
    );
  }

I have used this method but it doesn’t seems to work
what is the method if above on

Solution

I think you’re looking for this:

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

All code that needs the metadata needs to be in the then block.


Alternatively if you want to use await, you don’t need then():

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

Answered By – Frank van Puffelen

Answer Checked By – Jay B. (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *