flutter_cached 3.0.1

  • Readme
  • Changelog
  • Example
  • Installing
  • 69

When building an app that caches data, there are many special cases to think about.

For example, according to the Material Design guidelines, you need to worry about displaying offline data, handling swipe to refresh, showing error banners and displaying empty states.

This package tries to make implementing cached Flutter apps as easy as possible.

loading with no data in cacheloading with data in cacheloading successful
error with no data in cacheerror with data in cacheno data available

Usage #

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 CachedBuilder assumes that the builder returns a scrollable widget, like a ListView or GridView. If that's not the case, you need to set hasScrollBody to false in 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? #

The 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 update.

[3.0.1] - 2019-11-09

  • Fix 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 loadFromCache, the CacheController only catches 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.
  • The CacheUpdate now also contains a stackTrace if it contains an error. That makes debugging even easier.

[2.1.1] - 2019-11-02

  • You don't need to call dispose anymore. If you want to listen to multiple update streams of the same controller, just request updates multiple times.

[2.1.0] - 2019-11-02

  • Optionally provide a controllerBuilder instead of a controller to the CachedRawBuilder or the CachedBuilder. dispose is called automatically.

[2.0.2] - 2019-10-14

  • Provide lastData and lastError getters on CacheController.

[2.0.1] - 2019-10-12

  • Update examples.
  • Complete transition to flutter_cached.
  • Remove list_diff dependency.

[2.0.0] - 2019-10-12

  • Complete overhaul of the API.
  • Package moved to flutter_cached.

[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 AnimatedCrossFade.

[1.1.4] - 2019-10-11

  • Fix type propagation on CacheController's updates.

[1.1.3] - 2019-10-06

  • We now depend on the pull_to_refresh package so that we can start the refresh indicator programatically.
  • Fix missing meta dependency.

[1.1.2] - 2019-10-05

  • CachedListView now takes either an itemBuilder for building the items directly one after another in an index-agnostic deterministic way or it takes an itemSliverBuilder for 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 CachedCustomScrollView.

[1.1.0] - 2019-10-04

  • Add abstraction level below CachedListView: CachedCustomScrollView.
  • Rename CacheManager to CacheController.
  • Update readme to reflect changes and display screenshots in table.

[1.0.0] - 2019-10-04

  • Add CacheManager and CachedListView.

example/README.md

example #

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:

with Flutter:


$ 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:


import 'package:flutter_cached/flutter_cached.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
39
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
69
Learn more about scoring.

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

Platforms

Detected platforms: Flutter

References Flutter, and has no conflicting libraries.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
flutter 0.0.0
meta ^1.1.7 1.1.7 1.1.8
pull_to_refresh ^1.5.6 1.5.7
Transitive dependencies
collection 1.14.11 1.14.12
sky_engine 0.0.99
typed_data 1.1.6
vector_math 2.0.8
Dev dependencies
flutter_test