By stuckedoverflow


2019-02-11 09:12:18 8 Comments

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?

2 comments

@F-1 2019-02-11 09:55:00

EDIT: anmol.majhail's answer is better

Your fetchImage method needs to return a future, here's some pseudo code as guidence

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

Future<void> _fetchAddImageToList(int productId) async {
     //trycatch
    dynamic imgBinary = await _repository.fetchImage(productId);
    listImages.add(imgBinary);
}

Future<NetworkImage> fetchImage(int id) async {
    New NetworkImage img = new NetworkImage();
    //do your fetch work here
    return img;
}

@anmol.majhail 2019-02-11 09:58:10

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

@stuckedoverflow 2019-02-11 10:03:48

It works but how then make a difference? Can you elaborate a little?

@pskink 2019-02-11 10:04:20

or even more directly: _repository.fetchImage(productId).then(listImages.add);

Related Questions

Sponsored Content

2 Answered Questions

[SOLVED] Create rounded cached image in Flutter

  • 2018-06-20 09:33:10
  • dshukertjr
  • 2116 View
  • 8 Score
  • 2 Answer
  • Tags:   dart flutter

1 Answered Questions

1 Answered Questions

Is there a way to convert Future<List<String>> to List<dynamic>?

  • 2019-01-21 20:45:12
  • Ouardia
  • 89 View
  • 0 Score
  • 1 Answer
  • Tags:   dart flutter

0 Answered Questions

How to "casting the argument to 'dart.ui::Image'" in Flutter/Dart

  • 2018-10-02 20:53:42
  • UpaJah
  • 84 View
  • 0 Score
  • 0 Answer
  • Tags:   image dart flutter

1 Answered Questions

1 Answered Questions

1 Answered Questions

[SOLVED] How to convert FutureBuilder<File> to BoxDecoraiton Image

  • 2018-06-05 22:47:51
  • AhabLives
  • 873 View
  • 1 Score
  • 1 Answer
  • Tags:   dart flutter

0 Answered Questions

Future builder issue

  • 2018-06-01 12:56:10
  • Heyab Redda
  • 296 View
  • 0 Score
  • 0 Answer
  • Tags:   dart flutter

1 Answered Questions

[SOLVED] Flutter - Combine dynamically generated elements with hard-coded ones

  • 2017-06-01 11:55:02
  • OhMad
  • 2048 View
  • 3 Score
  • 1 Answer
  • Tags:   dart flutter

Sponsored Content