1

in this code which i used in my application i want to cache images during i scroll grid view, basically when i scroll grid view that cause reload again images and cause of getting speeding low during scroll

is any way to cache images before put them into GridView like with ImageCache?

StreamBuilder<List<MediaModel>>(
  stream: _globalBloc.storageMediaBloc.imagesMedia$,
  builder: (context, snapshot) {
    final List<MediaModel> _allImages = snapshot.data;
    _allImages.map((image) => mediaFoldersList.add(MediaDropDownStructure(image.folder, image.folder)));
    final MediaModel _all = _allImages[1];
    return GridView.builder(
      controller: scrollController,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        mainAxisSpacing: 5.0,
        crossAxisSpacing: 5.0,
      ), //change the numb
      itemBuilder: (context, index) {
        return AspectRatio(
            aspectRatio:1.0,
            child: Image(image: FileImage(File('${_all.files[index]}')),fit: BoxFit.cover,));
      },
      itemCount: _all.files.length,
    );
  }
),
3
  • just don't use the builder constructor, use the normal/unnamed constructor. Commented May 1, 2020 at 20:13
  • @hiwajalal what's your mean? i can't get your mean Commented May 1, 2020 at 20:15
  • this is for normal constructor => Creates a scrollable, 2D array of widgets with a custom SliverGridDelegate. this for the builder constructor => Creates a scrollable, 2D array of widgets that are created on demand. also take a look this Commented May 1, 2020 at 20:23

2 Answers 2

1

I'm not sure, try to move the file reading out of the grid builder:

  StreamBuilder<List<MediaModel>>(
  stream: _globalBloc.storageMediaBloc.imagesMedia$,
  builder: (context, snapshot) {
    final List<MediaModel> _allImages = snapshot.data;
    _allImages.map((image) => mediaFoldersList.add(MediaDropDownStructure(image.folder, image.folder)));
    final List<File> _all = _allImages[1].map((path) => File(path));

    return GridView.builder(
      controller: scrollController,
      gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
        crossAxisCount: 3,
        mainAxisSpacing: 5.0,
        crossAxisSpacing: 5.0,
      ), //change the numb
      itemBuilder: (context, index) {
        return AspectRatio(
            aspectRatio:1.0,
            child: Image(image: FileImage(_all[index]),fit: BoxFit.cover,));
      },
      itemCount: _all.files.length,
    );
  }
),
Sign up to request clarification or add additional context in comments.

1 Comment

it doesn't any change :(
0

I faced a similar issue as yours where images are constantly re-rendering even if the images are stored locally.

return AspectRatio(
        aspectRatio:1.0,
        child: Container(key: Key("1234"), child:Image(image: FileImage(File('${_all.files[index]}')),fit: BoxFit.cover,)))

I found that by adding a random key like Key("1234") would prevent the image from re-rendering.

Do try out the solution and let me know if it works.

1 Comment

i tested both of ${Random().nextInt(5)} and 1234, but it doesn't any change

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.