CHRISTOPANANJICKAL / fast_cached_network_image

A flutter package to cache network image fastly without native dependencies.
MIT License
30 stars 36 forks source link

Stuck in loadingBuilder? #3

Closed large closed 2 years ago

large commented 2 years ago

Thank you for sharing this great class! I have been using it on my app recently and it has worked fine most of the time.

Sometimes when you lay the app in the background and take it into front, the loadingBuilder() is called and I uses a greybox and it is stuck dere. I have to force setState() to make it show the picture. Is this something you have observered?

CHRISTOPANANJICKAL commented 2 years ago

Thanks for you feedback. Ill check and update.

large commented 2 years ago

As you can see in these two pictures, the grey square is there after reopening the app.

Screenshot_20220918-093534.png

Screenshot_20220918-093450.png

If I press the dropdown each picture is loaded as expected. When closing dropdown the gray square is still there. After maybe 5-10s the picture shows as expected. Cacheissue maybe?

CHRISTOPANANJICKAL commented 2 years ago

Can you share the code snippet?

large commented 2 years ago

Strange part is that sometimes it works fine. Could it be that it waits for reply from http or something? Even it has the picture cached... Dropdown is part of a bigger project, but here is how the data is loaded:

Init:

  void initState() async
  {
    var directory = await getApplicationDocumentsDirectory();
    var folderDirectory = Directory('${directory.path}/cached_images/');
    await FastCachedImageConfig.init(
        path: folderDirectory.path, clearCacheAfter: const Duration(days: 7));
  }

Drop down:

      FutureBuilder<List<CountryModel>>(
          future: _snatchCountry,
          builder: (context, snap) {

            //No love if the data isn't available yet
            if (!snap.hasData ||
                snap.connectionState == ConnectionState.active ||
                snap.connectionState == ConnectionState.waiting) {
              return const Center(
                child: SizedBox(height: 50,child: CircularProgressIndicator()),
              );
            }

            //Return the love
            return Container(
              child: DropdownButton<CountryModel>(
                iconSize: 0.0,
                value: selectedCountry,
                isExpanded: true,
                elevation: 16,
                underline: const SizedBox(),
                dropdownColor: Theme.of(context).primaryColor,
                alignment: AlignmentDirectional.center,
                style: Theme.of(context).textTheme.bodyText1,
                items: snap.data?.map((CountryModel country) {
                  return DropdownMenuItem<CountryModel>(
                    value: country,
                    alignment: AlignmentDirectional.center,
                    child:
                    SizedBox(
                      width: 52,
                      height: 35,
                      child: 
                      FastCachedImage(
                        url: "https://${widget.endPoint}/${country.flagImage!}",
                        fit: BoxFit.cover,
                        fadeInDuration: const Duration(milliseconds: 0),
                        errorBuilder: (context, exception, stacktrace) {
                          return Text(exception.toString());
                        },
                        loadingBuilder: (context) {
                          return Container(color: Colors.grey);
                        },
                      ),
                    )
                  );
                }).toList(),
                onChanged: onStateChange,
              ),
            );
          });
CHRISTOPANANJICKAL commented 2 years ago

Please try moving this code from the init to the main function.

var directory = await getApplicationDocumentsDirectory(); var folderDirectory = Directory('${directory.path}/cached_images/'); await FastCachedImageConfig.init( path: folderDirectory.path, clearCacheAfter: const Duration(days: 7));

You must not use this code anywhere else in the app other than main. You need to initialize the fastcacheimageconfig only once. Please try this and update.

Also please try adding a duration like 300 or 400 milliseconds for fadeInDuration property

large commented 2 years ago

Ahh, there was my error. Moved the init to main and everything works as expected. The fade is an effect I do not want to have, so for me it should be optional.

Thanks for a great widget, it will show flags for alot of users these days :)