mchome / flutter_advanced_networkimage

flutter advanced network image provider
https://pub.dev/packages/flutter_advanced_networkimage
MIT License
285 stars 180 forks source link

User initiated retry image load? #66

Closed eggman87 closed 5 years ago

eggman87 commented 5 years ago

If I re render a new AdvancedNetworkImage after a failure with the same URL that failed, the image does not even seem to attempt to load (...seems like same old widget stays put). The use case I am trying to solve is reloading the image after the user manually requests it via a button tap (as opposed to auto-retry).

I can get around this by manually adding a header to the AdvancedNetworkImage that increments with a local retry count but it seems like a bit of a hack.

Is this kind of use case possible without manipulating the parameters? Below is code for reference.

For the record, I am fairly new to flutter, guessing that because the final values of AdvancedNetworkImage not changing between new instances is causing some sort of caching/ignoring somewhere because it is being treated as a equal instance. Sorry if this is a bad question

class GalleryImageViewState extends State<GalleryImageView> {
  final String imageUrl;

  bool isFailed = false;
  int retryCount = 0;

  GalleryImageViewState(this.imageUrl);

  @override
  Widget build(BuildContext context) {
    if (isFailed) {
      return Container(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            Text("error loading image", style: TextStyle(color: Colors.white)),
            FlatButton(
              child: Text("Retry"),
              color: Colors.white,
              onPressed: _retry,
            )
          ],
        ),
      );
    } else {
      return TransitionToImage(
        image: AdvancedNetworkImage('$imageUrl',
            header: {'x-retry-count': '$retryCount'},
            useDiskCache: true,
            retryLimit: 1,
            printError: true,
            loadFailedCallback: _loadFailed),
        enableRefresh: true,
        loadingWidgetBuilder: (progress) => Center(
              child: CircularProgressIndicator(
                backgroundColor: Colors.blue,
              ),
            ),
      );
    }
  }

  void _loadFailed() {
    setState(() {
      isFailed = true;
    });
  }

  void _retry() {
    setState(() {
      retryCount++;
      isFailed = false;
    });
  }
}
mchome commented 5 years ago

Try disableMemoryCache: true in TransitionToImage.

eggman87 commented 5 years ago

Confirmed that does work, thanks.