provide_it 0.16.0 copy "provide_it: ^0.16.0" to clipboard
provide_it: ^0.16.0 copied to clipboard

Provider-like state binding, management, and injection using only context extensions.

example/lib/main.dart

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

final messengerKey = GlobalKey<ScaffoldMessengerState>();
final pathParameters = <String, String>{
  'counterId': 'my-counter-id',
};
void main() {
  runApp(
    ProvideIt(
      // Auto-injects dependencies
      provide: (context) {
        context.provide(CounterService.init); // <- async
        context.provide(CounterRepository.new);
        context.provide(Counter.new);
      },
      // Auto-injects path parameters
      locator: (param) => pathParameters[param.name],

      // ProvideIt will take care of loading/error, but you can customize it:
      // - loadingBuilder: (context) => (...),
      // - errorBuilder: (context, error, stackTrae) =>ce(...),
      child: MaterialApp(
        scaffoldMessengerKey: messengerKey,
        home: Scaffold(
          body: Builder(
            builder: (context) {
              context.listen<Counter>((counter) {
                messengerKey.currentState?.hideCurrentSnackBar();
                messengerKey.currentState?.showSnackBar(
                  SnackBar(
                    content: Text('Counter changed: ${counter.count}'),
                  ),
                );
              });

              return Center(
                child: ElevatedButton(
                  onPressed: () {
                    showDialog(
                      context: context,
                      builder: (context) {
                        final counter = context.watch<Counter>();

                        return AlertDialog(
                          title: Text('Works in a dialog!'),
                          content: Text('${counter.count}'),
                          actions: [
                            TextButton(
                              onPressed: () => counter.increment(),
                              child: const Text('Increment'),
                            ),
                            TextButton(
                              onPressed: () => Navigator.of(context).pop(),
                              child: const Text('Close'),
                            ),
                          ],
                        );
                      },
                    );
                  },
                  child: Text('Open Counter dialog'),
                ),
              );
            },
          ),
        ),
      ),
    ),
  );
}

class CounterService {
  CounterService();

  static Future<CounterService> init() async {
    await Future.delayed(Duration(seconds: 3));
    return CounterService();
  }
}

class CounterRepository {
  CounterRepository(this.service);
  final CounterService service;
}

class Counter extends ChangeNotifier {
  Counter(this.repository, {required this.counterId});
  final CounterRepository repository;
  final String counterId;

  int _count = 0;
  int get count => _count;

  void increment() {
    _count++;
    notifyListeners();
  }
}
0
likes
0
points
234
downloads

Publisher

unverified uploader

Weekly Downloads

Provider-like state binding, management, and injection using only context extensions.

Homepage
Repository (GitHub)
View/report issues

Topics

#provider #context #state #management #injection

License

unknown (license)

Dependencies

flutter

More

Packages that depend on provide_it