When building an app that caches data, there are many special cases to think about.
This package tries to make implementing cached Flutter apps as easy as possible.
|loading with no data in cache||loading with data in cache||loading successful|
|error with no data in cache||error with data in cache||no data available|
First, create a
CacheController. This will be the class that orchestrates the fetching of data.
var controller = CacheController<Item>( // Does the actual work and returns a Future<Item>. fetcher: _downloadData, // Asynchronously saves an Item to the cache. saveToCache: _saveToCache, // Asynchronously loads a Item from the cache. loadFromCache: _loadFromCache, );
Then, you can use a
CachedBuilder and provide builders for all the special cases:
CachedBuilder( controller: controller, errorBannerBuilder: (context, error, stackTrace) => ..., errorScreenBuilder: (context, error, stackTrace) => ..., builder: (context, item) => ..., ),
And that's it!
Note: By default, the
CachedBuilderassumes that the
builderreturns a scrollable widget, like a
GridView. If that's not the case, you need to set
falsein order for the swipe to refresh to work.
If you want to create a
CacheController on the fly, you can also provide a
controllerBuilder. Then, you'll also won't have to worry about disposing it.
CachedBuilder( controllerBuilder: () => CacheController( saveToCache: ..., loadFromCache: ..., fetcher: ..., ), errorBannerBuilder: (context, error, stackTrace) => ..., errorScreenBuilder: (context, error, stackTrace) => ..., builder: (context, item) => ..., ),
How does it work? #
CacheController offers an
updates stream of
CacheUpdates which is filled with updates once
controller.fetch() is called.
If you want to react to every update manually, you can use
CachedRawBuilder, which takes a builder with
context and an
[3.0.1] - 2019-11-09
CacheController, where the data would be saved to the cache but not being sent directly to the updates streams.
[3.0.0] - 2019-11-02
- When calling
NotInCacheExceptions, not all possible errors. That makes it simpler to debug, because you need to explicitly throw this error if the cache is empty. Other errors will still bubble up to the debugger.
CacheUpdatenow also contains a
stackTraceif it contains an
error. That makes debugging even easier.
[2.1.1] - 2019-11-02
- You don't need to call
disposeanymore. If you want to listen to multiple update streams of the same controller, just request
[2.1.0] - 2019-11-02
- Optionally provide a
controllerBuilderinstead of a
disposeis called automatically.
[2.0.2] - 2019-10-14
[2.0.1] - 2019-10-12
- Update examples.
- Complete transition to
[2.0.0] - 2019-10-12
- Complete overhaul of the API.
- Package moved to
[1.1.5] - 2019-10-11
- Add more examples to the demo. Now all abstraction levels are covered.
- Fix unbound vertical height issue caused by
[1.1.4] - 2019-10-11
- Fix type propagation on
[1.1.3] - 2019-10-06
- We now depend on the
pull_to_refreshpackage so that we can start the refresh indicator programatically.
- Fix missing meta dependency.
[1.1.2] - 2019-10-05
CachedListViewnow takes either an
itemBuilderfor building the items directly one after another in an index-agnostic deterministic way or it takes an
itemSliverBuilderfor transforming all the items into slivers simultaneously, allowing for changing the order, grouping etc of items.
[1.1.1] - 2019-10-04
- Fix type propagation for
[1.1.0] - 2019-10-04
- Add abstraction level below
- Update readme to reflect changes and display screenshots in table.
[1.0.0] - 2019-10-04
A new Flutter project.
Getting Started #
This project is a starting point for a Flutter application.
A few resources to get you started if this is your first Flutter project:
For help getting started with Flutter, view our online documentation, which offers tutorials, samples, guidance on mobile development, and a full API reference.
Use this package as a library
1. Depend on it
Add this to your package's pubspec.yaml file:
dependencies: flutter_cached: ^3.0.1
2. Install it
You can install packages from the command line:
$ flutter pub get
Alternatively, your editor might support
flutter pub get.
Check the docs for your editor to learn more.
3. Import it
Now in your Dart code, you can use:
Describes how popular the package is relative to other packages. [more]
Code health derived from static analysis. [more]
Reflects how tidy and up-to-date the package is. [more]
Weighted score of the above. [more]
We analyzed this package on Nov 11, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:
- Dart: 2.6.0
- pana: 0.12.21
- Flutter: 1.9.1+hotfix.6
Detected platforms: Flutter
References Flutter, and has no conflicting libraries.