Search code examples
dartflutterdynamictype

How to cast/convert Future<dynamic> into Image?


I have function to fetch image like

dynamic imgBinary = _repository.fetchImage(productId);

And I want to add this into List of Image

List<NetworkImage> listImages = new List<NetworkImage>();

So like

dynamic imgBinary = _repository.fetchImage(productId);
listImages.add(imgBinary);

How to cast this?


Solution

  • Ok , So you Can Try .then method.

    As _repository.fetchImage(productId); is Future.

    so you can try -

    List<NetworkImage> listImages = List<NetworkImage>();
        Future<dynamic> imgBinary = _repository.fetchImage(productId);
        imgBinary.then((i){
        listImages.add(i);
        });
    

    or

    Directly:

    _repository.fetchImage(productId).then((i){
    listImages.add(i);});
    

    To get the Value from Future - we can either use :

    async and await
    

    OR you can use the then() method to register a callback. This callback fires when the Future completes.

    For more info