Search code examples
flutterdartfirebase-storageflutter-state

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

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);