Skip to main content

cobalt_flutter 1.7.0 copy "cobalt_flutter: ^1.7.0" to clipboard
cobalt_flutter: ^1.7.0 copied to clipboard

Dependency injection for Flutter apps. Widgets own scopes, startup shows loading and error screens, and context.cobalt<T>() reads a dependency in any widget.

example/example.md

cobalt_flutter example #

CobaltAppScope owns the root scope for as long as the app is mounted. Its usual home is MaterialApp.builder, where the loading and error screens are rendered with the app's own theme.

import 'package:cobalt_flutter/cobalt_flutter.dart';
import 'package:flutter/material.dart';

void main() => runApp(
  MaterialApp(
    theme: ThemeData(colorSchemeSeed: Colors.indigo),
    builder: CobaltAppScope.builder(
      root: const AppScope(),
      rootName: 'app',
      loading: const Scaffold(body: Center(child: CircularProgressIndicator())),
      errorBuilder: (context, error, retry) => Scaffold(
        body: Center(
          child: Column(
            mainAxisSize: MainAxisSize.min,
            children: [
              Text('$error'),
              FilledButton(onPressed: retry, child: const Text('Try again')),
            ],
          ),
        ),
      ),
    ),
    home: const HomeScreen(),
  ),
);

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

  @override
  Widget build(BuildContext context) =>
      Scaffold(body: Center(child: Text('${context.cobalt<Database>()}')));
}

A scope that lives as long as one screen is CobaltScopeWidget; a full app with one screen per capability is examples/notes_app.

0
likes
160
points
1.6k
downloads

Documentation

API reference

Publisher

unverified uploader

Weekly Downloads

Dependency injection for Flutter apps. Widgets own scopes, startup shows loading and error screens, and context.cobalt<T>() reads a dependency in any widget.

Repository (GitHub)
View/report issues
Contributing

Topics

#dependency-injection #di #flutter #scopes

License

MIT (license)

Dependencies

cobalt, flutter

More

Packages that depend on cobalt_flutter