Alle Anleitungen

Quickstart

Add the packages, generate the service container plugin and boot your first container.

1. Install dependencies

First of all, you need to add the required dependencies.

To add the dependencies, simply run this command inside your dart project:

dart pub add dev:build_runner catalyst_builder catalyst_builder_contracts

2. Setting up the code generator

The code builder checks at build-time your code for specific annotations.

To generate the service container plugin you need to add the @GenerateServiceContainerPlugin to your main function.

// add this import
import 'package:catalyst_builder_contracts/catalyst_builder_contracts.dart';
import 'package:flutter/material.dart';

// add this line
@GenerateServiceContainerPlugin(pluginClassName: 'AppPlugin')
void main() {
  runApp(const MainApp());
}

class MainApp extends StatelessWidget {
  const MainApp({super.key});

  @override
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(body: Center(child: Text('Hello World!'))),
    );
  }
}

3. Generate the plugin

To generate the plugin for the service container that contains your services, you need to run the build runner

dart run build_runner build

After the command has been completed, you should see a *.catalyst_builder.plugin.g.dart file beneath the file you added the annotation.

4. Setting up the service container

After the plugin was generated, we can use it to set up the service container.

// add this import
import 'package:catalyst_builder/catalyst_builder.dart';
import 'package:catalyst_builder_contracts/catalyst_builder_contracts.dart';
import 'package:catalyst_builder_demo/main.catalyst_builder.plugin.g.dart';
import 'package:flutter/material.dart';

@GenerateServiceContainerPlugin(pluginClassName: 'AppPlugin')
void main() {
  // Create a new instance of the service container
  final container = ServiceContainer();
  // Register the services from our app
  container.useAppPlugin();
  // Boot the container
  container.boot();

  runApp(const MainApp());
}

// ...

Your project is now ready to use the service container. Head over to Your first service to learn more about service registration and loading.

Diese Seite auf GitHub bearbeiten