Genesis

genesis version genesis_generator version Pub likes Test on multiple Dart SDKs Runtime coverage

Genesis is a modular startup framework for Dart and Flutter. The name means “origin” or “beginning”. Genesis brings an app's modules together and gets their services ready to use.

Genesis applies the composition idea behind Docker Compose to mobile app startup. A Genesis container is a reusable Dart package that starts an SDK or feature in the app's process. The host app selects containers and supplies the configuration and adapters they need.

Why use Genesis containers?

  • Reuse SDK startup across apps. The container owns initialization. Each host app supplies its own options and adapters.
  • Start modules with runGenesis(). The generated runner wires dependencies by Dart type and runs tasks through configure, init, and execution. Tasks in the same phase can run concurrently when ready.
  • Check startup in CI. Doctor can check for missing dependencies across packages and export a Mermaid graph of startup tasks and their dependencies.

The runtime supports Android, iOS, macOS, Windows, Linux, Web, and OHOS with an OHOS-enabled SDK. It runs on Dart 3.8 or later, including projects without Flutter. The Doctor command runs on the Dart VM.

Billbook, a personal expense tracking and budgeting app for Android and iOS, uses Genesis in its production startup flow.

Before and after Genesis

These Flutter examples use generic task names to illustrate the startup structure used by Billbook.

Before Genesis

main.dart calls each initializer and wires the dependencies by hand.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  final settings = await openLocalSettings();
  final api = await createApiClient(settings);
  await loadFeatureFlags(api);
  await initializeMonitoring();
  await initializeSignIn(api);
  await initializePayments();
  await initializePush();
  await startAppServices();

  runApp(const MyApp());
}

With Genesis

When all startup tasks can run at launch, main.dart has one startup call.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await runGenesis();
  runApp(const MyApp());
}

lib/genesis.dart selects the container packages and includes app-specific configuration and tasks by feature. Each container owns its initialization logic and can be reused by another app.

app/lib/
├── main.dart
├── genesis.dart
├── genesis.g.dart             # generated
└── genesis/
    ├── feature_flags.dart
    └── app_services.dart
packages/
├── settings_container/
├── api_container/
├── monitoring_container/
├── sign_in_container/
├── payments_container/
└── push_container/

runGenesis() runs configure, init, and execution. Genesis resolves dependencies across packages. For example, the API client can consume local settings, and the feature flags task can consume the API client. Independent tasks in the same phase can run together.

Keep configure limited to work the app can do before consent, such as loading local settings for the app shell. Place SDK initialization that requires consent in init and workflows that depend on it in execution.

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await bootstrapWithConsent();
  runApp(const MyApp());
}

Future<void> bootstrapWithConsent() async {
  final hasConsent = await PrivacyConsent.isGranted();
  await runGenesisConfigure();

  if (hasConsent) {
    await runGenesisExecution();
  } else {
    await PrivacyConsent.runWhenGranted(runGenesisExecution);
  }
}

PrivacyConsent represents the app's consent flow. runWhenGranted() registers the callback and returns while consent is pending, so the shell can render. Once the user agrees, the callback runs init and execution, reusing the completed configure work. With saved consent, those phases run before runApp().

See the Flutter container guide for a complete workspace example.

Usage

Install the runtime and generator

Add genesis as an application dependency. Add the generator and build_runner as development dependencies. This setup requires Dart 3.9 or later.

dependencies:
  genesis: ^2.0.1

dev_dependencies:
  build_runner: ^2.7.1
  genesis_generator: ^2.0.1

Run dart pub get in your app directory. For a Flutter app, run flutter pub get instead.

Define and generate startup tasks

In lib/genesis.dart, declare the provider and the task that consumes it. Genesis uses the Greeting parameter type to link and inject the value.

import 'package:genesis/genesis.dart';

part 'genesis.g.dart';

/// The message printed by the startup task.
class Greeting {
  const Greeting(this.message);

  final String message;
}

@GenesisProvider()
Greeting _greeting() => const Greeting('Hello from Genesis!');

@GenesisTask(phase: GenesisTaskPhase.execution)
void _start(Greeting greeting) {
  print(greeting.message);
}

From your application package, generate lib/genesis.g.dart and the startup graph that Doctor reads.

dart run build_runner build

Run dart run build_runner watch to regenerate as you edit.

Start the application

From bin/main.dart, import the application's startup library and await the runner. Replace genesis_example with your application's package name.

import 'package:genesis_example/genesis.dart';

Future<void> main() async {
  await runGenesis();
}

Run dart run bin/main.dart to print Hello from Genesis!. In a Flutter app, initialize the Flutter binding and await Genesis before calling runApp().

Manual registration

For application-managed startup, add the runtime alone with dart pub add genesis or flutter pub add genesis.

Register providers and tasks with genesisRegisterPackageTasks, then call genesisRunExecution with the same package ID. Each task declares its typed dependencies in requires. Tasks can read those values with genesisReadProvider<T>() and publish a result using output.

The manual registration example loads a message catalog during init and uses it during execution. From the repository root, run the example with dart run genesis/example/main.dart after resolving dependencies with dart pub get.

Startup phases

Phase Entry point Typical use
Configure genesisRunConfigure Configure clients and wire adapters.
Init genesisRunInit Initialize shared services and storage.
Execution genesisRunExecution Start application workflows.

Each entry point also runs its prerequisite phases. Tasks in the same phase can run concurrently when their dependencies are ready. Successful work is reused on subsequent calls. Calling a phase again after a failure retries failed work.

  • Use requires for typed dependencies and provides for readiness tokens that do not carry a value. Use output when a task returns a value.
  • Providers are lazy by default. Set eager: true to construct a provider at startup. Eager providers cannot depend on task outputs.
  • Use GenesisCondition to select platforms or build modes. Make sure each selected task's dependencies are available in the same environment.
  • Execution tasks without outputs can use GenesisTaskSchedule.deferred or GenesisTaskSchedule.idle to schedule background work. Idle scheduling yields to the Dart event loop.

For multi-package startup, register each package and pass its direct Genesis package IDs in dependencies. Start the application by passing its package ID. Use genesisDescribePlan(packageId: ...) to inspect the registered graph and GenesisRunReport.toJson() to examine execution results.

Doctor

Doctor reads .dart_tool/genesis_graph.json from a code generation build. Run these commands from your application package after generating the graph.

dart run genesis:doctor --check
dart run genesis:doctor --graph

See the Doctor guide for JSON output and workspace checks.

Examples

Example What it shows
Generated startup A runnable Dart app with annotated tasks and Doctor.
Manual registration A provider, an init task, and an execution task wired by type.
Flutter containers A host app, a reusable SDK, and separate startup containers.

Documentation

Libraries

genesis
Typed, staged startup orchestration for Dart and Flutter applications.
testing