brick_core 0.0.5

  • Readme
  • Changelog
  • Example
  • Installing
  • 76

Brick Core #

Interfaces and shared helpers for implementing models, adapters, providers, and repositories in Brick.

Principles #

Brick's architecture encourages using data through a single access point: the repository.

Data flow with a single provider

A repository may implement multiple providers, but after instantiation the end application is only aware of the repository. The stream of data follows one course through each provider:

Data flow with many providers

The repository is aware of provider(s), the provider(s) are aware of the adapters, the adapters are aware of models, and the models are aware of only themselves. From the reverse, models are unaware, adapters are unaware of providers and providers are unaware of repositories.


Because models are atomic and unaware, they don't rely on (de)serializing functions (unlike with JSON serializable). Brick reduces the concerns of the end-implementation by hiding (de)serializing and fetching logic into adapters, which translate raw data between providers:


Unreleased #

0.0.5 #

  • Rename Query#params to Query#providerArgs, reflecting the much narrower purpose of the member

0.0.4 #

  • FieldSerializable#defaultValue changes from dynamic to String. As this is injected directly into the adapter, it does not need to be dynamic and should better reflect its purpose.

0.0.3+1 #

  • Moves generator placeholders to FieldSerializable form OfflineFirst
  • Removes query validation that ensures all Where conditions have a non-null value

0.0.3 #

  • Add And and Or Where subclasses
  • Removes Type argument from Where
  • Adds semantic methods to Where such as isExactly and
  • BREAKING Revise Where syntax. This removes the second positional argument in favor of using it in a semantic method.
  • Adds Where.exact factory to preserve previous, short syntax
  • Move query-related files to src/query and make them accessible from a barrel file in the lib root

0.0.2 #

  • Fix linter hints
  • Adds initialize method to ModelRepository. This is enforces a predictable, overridable method for sub classes


import 'dart:convert';
import 'dart:io';

import 'package:glob/glob.dart';

import '../lib/core.dart';

abstract class FileModel extends Model {
  String get fileName;

  Future<dynamic> get contents async {
    final contents = await asFile.readAsString();
    if (contents.startsWith('{')) return jsonDecode(contents);
    return contents;

  File get asFile => File(fileName);

class User extends FileModel {
  String get fileName => name;

  final String name;


class FileProvider implements Provider<FileModel> {
  final FileModelDictionary modelDictionary;

  delete<T extends FileModel>(instance, {query, repository}) async =>
      await instance.asFile.delete();

  /// Query.where must always include `filePath`
  Future<List<T>> get<T extends FileModel>({query, repository}) async {
    final adapter = modelDictionary.adapterFor[T];
    if (query.where != null) {
      final filePath = Where.firstByField('filePath', query.where)?.value;

      final contents = await File('${}/$filePath.json').readAsString();
      return [await adapter.fromFile(contents)];

    final files = Glob('${}/**.${adapter.fileExtension}');
    return Future.wait<T>(files.listSync().map<Future<T>>((file) async {
      final contents = await File(file.path).readAsString();
      return await adapter.fromFile(contents);

  Future<T> upsert<T extends FileModel>(instance, {query, repository}) async {
    final adapter = modelDictionary.adapterFor[T];
    final fileContents = await adapter.toFile(instance, provider: this, repository: repository);
    await File(instance.fileName).writeAsString(fileContents);
    return instance;

abstract class FileAdapter<_Model extends FileModel> extends Adapter<_Model> {
  /// Folder to store all of these
  String get directory;

  String get fileExtension => '.json';

  String filePath(String fileName) => '$directory/$fileName$fileExtension';

  Future<FileModel> fromFile(
    String data, {
    FileProvider provider,
    ModelRepository<FileModel> repository,
  Future<String> toFile(
    _Model instance, {
    FileProvider provider,
    ModelRepository<FileModel> repository,

/// This is generated. As this is an example, `FileProvider` does not
/// have a complimenting build system to generate this adapter. It was handwritten
/// for this example.
class UserAdapter extends FileAdapter<User> {
  final directory = 'users';


  Future<User> fromFile(input, {provider, repository}) async {
    final contents = jsonDecode(input);
    return User(name: contents['name']);

  Future<String> toFile(instance, {provider, repository}) async =>

class FileModelDictionary extends ModelDictionary<FileModel, FileAdapter> {
  FileModelDictionary(Map<Type, FileAdapter> dictionary) : super(dictionary);

final Map<Type, FileAdapter> mappings = {
  User: UserAdapter(),
final fileModelDictionary = FileModelDictionary(mappings);

// finally, what the end application sees:
class FileRepository extends SingleProviderRepository<FileModel> {
  FileRepository() : super(FileProvider(modelDictionary: fileModelDictionary));

void main() async {
  final repository = FileRepository();

  await repository.upsert<User>(User(name: 'Thomas'));

  final users = await repository.get<User>(query: Query.where('fileName', 'Thomas'));
  await repository.delete<User>(users.first);

Use this package as a library

1. Depend on it

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

  brick_core: ^0.0.5

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:brick_core/core.dart';
import 'package:brick_core/field_serializable.dart';
import 'package:brick_core/query.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 Apr 3, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.7.1
  • pana: 0.13.6

Health suggestions

Fix lib/src/query/where.dart. (-0.50 points)

Analysis of lib/src/query/where.dart reported 1 hint:

line 98 col 27: Avoid const keyword.

Format lib/src/model_repository.dart.

Run dartfmt to format lib/src/model_repository.dart.

Format lib/src/provider.dart.

Run dartfmt to format lib/src/provider.dart.

Format lib/src/query/query.dart.

Run dartfmt to format lib/src/query/query.dart.

Maintenance suggestions

Package is pre-v0.1 release. (-10 points)

While nothing is inherently wrong with versions of 0.0.*, it might mean that the author is still experimenting with the general direction of the API.


Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
collection ^1.14.11 1.14.12
Dev dependencies
dart_style ^1.2.4
mockito ^4.0.0
test >=1.9.4 <2.0.0