repository 1.0.3

When you have a large project, you probably want to manage data—you need to store it, fetch it from a server etc.

The repository package introduces a bottom-up high-level data management abstraction layer that lets you do just that!

There are only a few key concepts:

Repositories can store objects.
Objects which are stored in repositories are called items.
Among all items in a repository, an item can be uniquely identified by its ID.

So, what can repositories do? #

  • Every repository can fetch items, returning a stream of items to the caller. Oftentimes, the stream will just contain one item, but there are times when it's very useful to return a sequence of items.
  • Some repositories are finite, so you can fetchAll the items.
  • Some repositories are mutable, so you can update items.

There are already some repositories implemented. Here's just a quick look at three very different ones:

  • InMemoryStorage is a mutable and finite repository, allowing you to store objects in the device's memory.
  • JsonToStringTransformer stores items by serializing them and saving them to a provided Repository<String>.
  • The CachedRepository accepts a source and a cache repository. When fetching items, it first returns the item from the cache and returns the item from the source afterwards.
  • Of course, you can also create your own repositories, i.e. one that downloads data from a server when items are fetched.

By now, you probably realized that all of these repositories only do very simple, deterministic tasks. Because that makes repositories modular, they can be composed into more powerful, higher-level repositories that elegantly handle the complex organization of streams from a variety of sources.

Here's an example of how that might look:

repository chains

  source: ArticleDownloader(),
  cache: ObjectToJsonTransformer(
    serializer: ArticleSerializer(),
    source: JsonToStringTransformer(
      source: SharedPreferencesStorage(keyPrefix: 'articles'),

If you're familiar with Flutter widgets, you'll immediately notice the style that favors composition over inheritance, allowing for incredibly flexible structures and abstractions.

[1.0.3] - 2019-09-13

  • Major rewrite of CachedRepository to be more efficient and gracefully handle the different cache strategies as well as caches yielding multiple values.

[1.0.2] - 2019-09-09

  • CachedRepository allowing multiple cache strategies.

[1.0.1] - 2019-09-09

  • Minor fixes allowing fetch stream subscription to be closed after first event.

[1.0.0] - 2019-09-06

  • Removed unnecessary dependencies flutter, hive, path_provider, provider and shared_preferences.
  • Fixed pubspec description and homepage.
  • Added example.

[0.0.1] - 2019-08-22

  • Initial release with basic Repository and Id structure, as well as InMemoryStorage, Transformer, ObjectToJsonTransformer, JsonToStringTransformer, and CachedRepository.

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:

  repository: ^1.0.3

2. Install it

You can install packages from the command line:

with pub:

$ pub get

with Flutter:

$ flutter pub get

Alternatively, your editor might support pub get or 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:repository/repository.dart';
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]
Learn more about scoring.

We analyzed this package on Sep 13, 2019, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.5.0
  • pana: 0.12.21


Detected platforms: Flutter, web, other

No platform restriction found in primary library package:repository/repository.dart.

Health suggestions

Fix lib/src/repositories/cached_repository.dart. (-1 points)

Analysis of lib/src/repositories/cached_repository.dart reported 2 hints:

line 78 col 9: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

line 140 col 7: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Fix lib/src/repositories/in_memory_storage.dart. (-0.50 points)

Analysis of lib/src/repositories/in_memory_storage.dart reported 1 hint:

line 45 col 5: Future results in async function bodies must be awaited or marked unawaited using package:pedantic.

Fix lib/src/repositories/transformer.dart. (-0.50 points)

Analysis of lib/src/repositories/transformer.dart reported 1 hint:

line 29 col 11: DO use curly braces for all flow control structures.

Maintenance suggestions

Maintain an example. (-10 points)

Create a short demo in the example/ directory to show how to use this package.

Common filename patterns include main.dart, example.dart, and repository.dart. Packages with multiple examples should provide example/

For more information see the pub package layout conventions.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.2.2 <3.0.0
meta ^1.1.7 1.1.7
rxdart ^0.22.0 0.22.2