0.4.1

Configuration FAQ

Does Flutter Data depend on Flutter?

No! Despite its name this library does not depend on Flutter at all.

See the example folder for an, uh, example.

Initializer arguments

This is the signature of the generated dataProviders method:

List<SingleChildWidget> dataProviders(
  Future<Directory> Function() directory,
  {
    bool clear,
    bool remote,
    bool verbose,
    List<int> encryptionKey
  }
);

Arguments:

  • directory: conveniently takes a “lazy” function that returns a Future<Directory> for local storage (Hive boxes); such that the Flutter/Provider async initialization can be entirely performed by Flutter Data (and prevent any async work prior to that). Required.
  • clear: whether to delete the resolved directory with all Hive boxes. Defaults to true (until Flutter Data 1.0.0, then it will default to false).
  • remote: passing false will make all repositories work exclusively on local storage (no remote requests will be made). This setting can be overriden at the method level. Defaults to true.
  • verbose: whether to print Flutter Data (minimal) logs or not. Defaults to true.
  • encryptionKey: if this 256-bit key is supplied, all Hive boxes will be AES encrypted. Optional, defaults to null.

All these are eventually passed into the following FlutterData.init method:

static Future<DataManager> init(
  Directory baseDir,
  {
    bool clear,
    bool remote,
    bool verbose,
    List<int> encryptionKey,
    Function(void Function<R>(R)) also
  }
);

Additional arguments:

  • baseDir on the other hand requires a Directory and assumes any async directory retrieval will be done prior to calling this method, like dataProviders does.
  • also: allows additional external services to be registered with the internal service locator. Very useful when data are required in the context of initializing Flutter Data, such as retreiving an auth token from an external source in order to set the default repository headers.

See below for examples of initializing Flutter Data via FlutterData.init directly.

Configuration without Provider

// main.dart

import 'package:flutter/material.dart';
import 'package:flutter_data/flutter_data.dart';
import 'package:path_provider/path_provider.dart';
import 'package:todo_app/main.data.dart';

void main() {
  runApp(Center(child: const CircularProgressIndicator()));

  final baseDir = await getApplicationDocumentsDirectory();
  final manager = await FlutterData.init(baseDir);
  Locator locator = manager.locator;

  runApp(MaterialApp(
    // ...
    final repository = locator<Repository<User>>();
    // ...
  ));
}

Locator is defined like this:

typedef Locator = T Function<T>();

Any conforming type can be used:

  • the bundled locator shown above
  • a get_it locator
  • context.read from the Provider package

Configuration for pure Dart projects (no Flutter)

One way of doing it:

void main() async {
  Directory _dir;

  try {
    _dir = await Directory('/tmp/myapp').create();
    final manager = await FlutterData.init(_dir);
    Locator locator = manager.locator;
    
    final repository = locator<Repository<User>>();
    
    // ...

  } finally {
    await _dir.delete(recursive: true);
  }
}