user1255603
user1255603

Reputation: 557

Lazy load of images in Flutter?

My app loads a new image from the internet as a background image for the screen when the user taps on a FAB. I can determine what URL to load prior to the user tapping the screen.

Is there a way to lazy load an image prior to it needing to be displayed by FadeInImage or another Flutter Widget image display mechanism?

Upvotes: 3

Views: 12148

Answers (1)

Jonah Williams
Jonah Williams

Reputation: 21441

You can load an Image ahead of being request in the ui with the precacheImage method. Any future requests for a matching image - the same url in the case of a network image - will pull from the cache. To load a network image, you can pass it an instance of the NetworkImage class.

Example:

class SomeWidgetState extends State<SomeWidget> {

  @override
  void initState() {
    super.initState();
    precacheImage(NetworkImage('someurl.jpg'));
  }

  ...
}

Upvotes: 4

Related Questions