depend 3.0.0-dev copy "depend: ^3.0.0-dev" to clipboard
depend: ^3.0.0-dev copied to clipboard

InjectionScope is a library for managing dependencies in Flutter applications. It provides a convenient way to initialize and access services or repositories via an InheritedWidget.

example/lib/main.dart

import 'package:depend/depend.dart';
import 'package:example/src/default_bloc.dart';
import 'package:flutter/material.dart';
import 'package:flutter_bloc/flutter_bloc.dart';

class RootInjection extends Injection {
  late final ApiService apiService;

  @override
  Future<void> init() async {
    apiService = await ApiService().init();
  }

}

class ModuleInjection extends Injection<RootInjection> {
  late final AuthRepository authRepository;

  ModuleInjection({required super.parent});

  @override
  Future<void> init() async {
    authRepository = AuthRepository(
      dataSource: AuthDataSource(
        apiService: parent.apiService,
      ),
    );
  }

  @override
  void dispose() {
    authRepository.dispose();
  }
}

void main() {
  runApp(
    InjectionScope<RootInjection>(
      injection: RootInjection(),
      placeholder: const ColoredBox(
        color: Colors.white,
        child: Center(child: CircularProgressIndicator()),
      ),
      child: const MyApp(),
    ),
  );
}

/// The API service for the example
class ApiService {
  ApiService();

  Future<ApiService> init() async {
    return Future.delayed(const Duration(seconds: 1), () => this);
  }
}

/// The data source for the example
class AuthDataSource {
  final ApiService apiService;

  AuthDataSource({required this.apiService});

  Future<String> login() => Future.value('Token');
}

/// The repository for the example
final class AuthRepository {
  final AuthDataSource dataSource;

  AuthRepository({required this.dataSource});

  Future<String> login() => dataSource.login();

  void dispose() {
    // stream.close();
  }
}

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

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      home: InjectionScope<ModuleInjection>(
        injection: ModuleInjection(
          parent: InjectionScope.of<RootInjection>(context),
        ),
        child: BlocProvider(
          create: (context) => DefaultBloc(
            InjectionScope.of<ModuleInjection>(context).authRepository,
          ),
          child: const MyHomePage(),
        ),
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  void _login() {
    context.read<DefaultBloc>().add(DefaultEvent());
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: SingleChildScrollView(
          child: Column(
            children: [
              BlocBuilder<DefaultBloc, DefaultState>(
                builder: (context, state) {
                  return Text('Login: ${state.authorized}');
                },
              ),
              Builder(
                builder: (context) {
                  return ElevatedButton(
                    onPressed: _login,
                    child: const Text('Login'),
                  );
                },
              )
            ],
          ),
        ),
      ),
    );
  }
}
5
likes
0
points
81
downloads

Publisher

verified publishercontributors.info

Weekly Downloads

InjectionScope is a library for managing dependencies in Flutter applications. It provides a convenient way to initialize and access services or repositories via an InheritedWidget.

Homepage
Repository (GitHub)
View/report issues

Topics

#dependency #injection #dependency-injection #service #dependency-management

License

unknown (license)

Dependencies

flutter

More

Packages that depend on depend