2025-09-08 17:16:51 +03:00
[](https://github.com/pese-git/cherrypick/actions/workflows/pipeline.yml)
2025-09-09 13:22:39 +03:00
[](https://app.netlify.com/projects/cherrypick-di/deploys)
2025-09-08 17:16:51 +03:00
---
2025-05-02 11:42:32 +03:00
# CherryPick Flutter
2025-05-14 10:23:29 +03:00
`cherrypick_flutter` offers a Flutter integration to access and manage dependency injection scopes using the `CherryPickProvider` . This setup facilitates accessing the root scope directly from the widget tree, providing a straightforward mechanism for dependences management within Flutter applications.
2025-05-02 11:42:32 +03:00
## Installation
Add `cherrypick_flutter` to your `pubspec.yaml` :
```yaml
dependencies:
cherrypick_flutter: ^1.0.0
```
2025-05-14 10:23:29 +03:00
Run `flutter pub get` to install the package dependencies.
2025-05-02 11:42:32 +03:00
## Usage
### Importing the Package
2025-05-14 10:23:29 +03:00
To begin using `cherrypick_flutter` , import it within your Dart file:
2025-05-02 11:42:32 +03:00
```dart
import 'package:cherrypick_flutter/cherrypick_flutter.dart';
```
### Providing State with `CherryPickProvider`
2025-05-14 10:23:29 +03:00
Use `CherryPickProvider` to encase the widget tree section that requires access to the root or specific subscopes:
2025-05-02 11:42:32 +03:00
```dart
import 'package:flutter/material.dart';
import 'package:cherrypick_flutter/cherrypick_flutter.dart';
void main() {
runApp(
CherryPickProvider(
child: MyApp(),
),
);
}
```
2025-05-14 10:23:29 +03:00
Note: The current implementation of `CherryPickProvider` does not directly pass a `rootScope` . Instead, it utilizes its methods to open root and sub-scopes internally.
2025-05-02 11:42:32 +03:00
### Accessing State
2025-05-14 10:23:29 +03:00
Access the state provided by `CherryPickProvider` within widget `build` methods using the `of` method:
2025-05-02 11:42:32 +03:00
```dart
class MyWidget extends StatelessWidget {
@override
Widget build(BuildContext context) {
2025-05-14 10:23:29 +03:00
final CherryPickProvider cherryPick = CherryPickProvider.of(context);
final rootScope = cherryPick.openRootScope();
// Use the rootScope or open a subScope as needed
final subScope = cherryPick.openSubScope(scopeName: "exampleScope");
2025-05-02 11:42:32 +03:00
2025-05-14 10:23:29 +03:00
return Text('Scope accessed!');
2025-05-02 11:42:32 +03:00
}
}
```
### Updating State
2025-05-14 10:23:29 +03:00
The `CherryPickProvider` setup internally manages state updates. Ensure the `updateShouldNotify` method accurately reflects when the dependents should receive updates. In the provided implementation, it currently does not notify updates automatically.
2025-05-02 11:42:32 +03:00
## Example
2025-05-14 10:23:29 +03:00
Here is an example illustrating how to implement and utilize `CherryPickProvider` within a Flutter application:
2025-05-02 11:42:32 +03:00
```dart
class MyApp extends StatelessWidget {
const MyApp({super.key});
@override
Widget build(BuildContext context) {
2025-05-14 10:23:29 +03:00
final rootScope = CherryPickProvider.of(context).openRootScope();
2025-05-02 11:42:32 +03:00
return MaterialApp.router(
routerDelegate: rootScope.resolve< AppRouter > ().delegate(),
routeInformationParser:
rootScope.resolve< AppRouter > ().defaultRouteParser(),
);
}
}
```
2025-05-14 10:23:29 +03:00
In this example, `CherryPickProvider` accesses and resolves dependencies using root scope and potentially sub-scopes configured by the application.
2025-05-02 11:42:32 +03:00
## Contributing
2025-05-14 10:23:29 +03:00
Contributions to improve this library are welcome. Feel free to open issues and submit pull requests on the repository.
2025-05-02 11:42:32 +03:00
## License
2025-08-08 23:24:05 +03:00
This project is licensed under the Apache License 2.0. A copy of the license can be obtained at [Apache License 2.0 ](https://www.apache.org/licenses/LICENSE-2.0 ).