MarcelGarus / flutter_cached

🧾 Flutter widget allowing easy cache-based data display in a ListView featuring pull-to-refresh and error banners.
BSD 3-Clause "New" or "Revised" License
17 stars 2 forks source link

fetcher is called on every flutter build() #5

Closed Nico04 closed 4 years ago

Nico04 commented 4 years ago

Since 4.2.5, the fecther is called on every flutter build(), which is not desired. It should only be called on initState, pull-to-refresh or if controller.fetch() is manually called.

Maybe it's because of the fix of #4.

To reproduce using your example, you can add an action button to the CachedBuildDemo's scaffold's appbar :

actions: <Widget>[
          IconButton(
            icon: Icon(Icons.add),
            onPressed: () => Navigator.of(context).push(MaterialPageRoute(
              builder: (_) => Scaffold(
                body: Text('nothing'),
              ),
            )),
          )
        ],

When you tap on it, it will open a new page, flutter will call CachedBuildDemo's build(), and fetcher will be called (not desired).

MarcelGarus commented 4 years ago

Ooo, you're right. We should probably only call it the first time. I'll have a look at it later today.

Actually, the current behavior is somewhat intended as a temporary behavior. In the long term, I was thinking about implementing multiple caching strategies that you can choose from (for example, only fetching the first time or fetching only if the last fetch is some amount in the past)

Nico04 commented 4 years ago

Implementing multiple caching strategies as you said is a great idea, but still, in anycase it should NOT be called on rebuild.

As Flutter's documentation say :

A general guideline is to assume that every build method could get called every frame.

So maybe before you add this multiple caching strategies implementation, you could just fix this ? Thanks :)

MarcelGarus commented 4 years ago

Yes, I'll fix it later today 😊

MarcelGarus commented 4 years ago

Got fixed with version 4.2.6, which is already live on pub.dev.

Nico04 commented 4 years ago

It fixes the build issue, but now I can't pull-to-refresh anymore, is that normal ?

MarcelGarus commented 4 years ago

I tested pull-to-refresh on my device and it worked…

Nico04 commented 4 years ago

You're right : it's another issue.