Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is making two simultaneous requests to ExtendedImage.network for the same image bad? #535

Open
mark8044 opened this issue Dec 13, 2022 · 2 comments

Comments

@mark8044
Copy link

I have created a FutureBuilderthat will get a network image and display it. But I also need image dimensions for a separate process. So I have created two separate futures which will work in the FutureBuilder:

FutureBuilder

        FutureBuilder<List>(
          future: Future.wait([
            getImageWidget(image:widget.image, width:widget.width, height:widget.height),
            _calculateImageDimension(image:widget.image),
          ]),

Future 1: Retrieve the image

Future<Widget> getImageWidget({image, width, height})
{
return ExtendedImage.network(...)
}

Future 2: Get the image dimensions

Future<Size> _calculateImageDimension({image}) {
  Completer<Size> completer = Completer();
  Image image = Image(image: ExtendedNetworkImageProvider(imgixedImage, cache: true));
  image.image.resolve(const ImageConfiguration()).addListener(
    ImageStreamListener(
          (ImageInfo image, bool synchronousCall) {
        var myImage = image.image;
        Size size = Size(myImage.width.toDouble(), myImage.height.toDouble());
        completer.complete(size);
      },
    ),
  );
  return completer.future;
}

I believe both of these futures are called simultaneously on the same image. I do not want to make two separate network requests for the same image, I was hoping one will go first and save the image in the cache and then the second one will just use the image retrieved from the cache.

Is ExtendedNetworkImage smart enough to deal with two simultaneous requests? Or is there a chance that there will be two separate downloads of the same image?

@zmtzawqlp
Copy link
Member

zmtzawqlp commented Dec 13, 2022

I don't know your goal, maybe there is a better way. you can get width/height from loadStateChanged (LoadState.completed)

@mark8044
Copy link
Author

mark8044 commented Dec 13, 2022

I don't know your goal, maybe there is a better way. you can get width/height from loadStateChanged (LoadState.completed)

In my case, using LoadState.completed gives the proper image dimension, but because I have to modify the scrollcontroller position with the result I get one of those setState() or markNeedsBuild called during build errors. I don't want to put it in a postcallframe callback because then there is a one frame lag and creates a bad UI experience.

Doing it as two futures that wait for each other works perfectly, except that Im afraid it might be doing two network calls?

If there aren't two network calls with such an approach, then problems are solved

Here is a more complex SO post with more code: https://stackoverflow.com/questions/74766860/how-do-you-update-a-value-derived-from-a-remote-source-that-updates-with-state

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants