Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: update to flutter bloc v9.1.0 #4368

Merged
merged 3 commits into from
Mar 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
---
import { Code } from '@astrojs/starlight/components';

const code = `
RepositoryProvider<RepositoryA>(
create: (context) => RepositoryA(),
dispose: (repository) => repository.dispose(),
child: ChildA(),
);
`;
---

<Code code={code} lang="dart" />
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,16 @@ import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:weather_repository/weather_repository.dart';

class WeatherApp extends StatelessWidget {
const WeatherApp({Key? key, required WeatherRepository weatherRepository})
: _weatherRepository = weatherRepository,
super(key: key);

final WeatherRepository _weatherRepository;
const WeatherApp({super.key});

@override
Widget build(BuildContext context) {
return RepositoryProvider.value(
value: _weatherRepository,
return RepositoryProvider(
create: (_) => WeatherRepository(),
dispose: (repository) => repository.dispose(),
child: BlocProvider(
create: (_) => ThemeCubit(),
child: WeatherAppView(),
create: (context) => WeatherCubit(context.read<WeatherRepository>()),
child: const WeatherAppView(),
),
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,8 @@ import { Code } from '@astrojs/starlight/components';
const code = `
import 'package:flutter/material.dart';
import 'package:flutter_weather/app.dart';
import 'package:weather_repository/weather_repository.dart';

void main() {
runApp(WeatherApp(weatherRepository: WeatherRepository()));
}
void main() => runApp(const WeatherApp());
`;
---

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class WeatherRepository {
condition: weather.weatherStateAbbr.toCondition,
);
}

void dispose() => _weatherApiClient.close();
}
`;
---
Expand Down
21 changes: 15 additions & 6 deletions docs/src/content/docs/flutter-bloc-concepts.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import BlocConsumerSnippet from '~/components/concepts/flutter-bloc/BlocConsumer
import BlocConsumerConditionSnippet from '~/components/concepts/flutter-bloc/BlocConsumerConditionSnippet.astro';
import RepositoryProviderSnippet from '~/components/concepts/flutter-bloc/RepositoryProviderSnippet.astro';
import RepositoryProviderLookupSnippet from '~/components/concepts/flutter-bloc/RepositoryProviderLookupSnippet.astro';
import RepositoryProviderDisposeSnippet from '~/components/concepts/flutter-bloc/RepositoryProviderDisposeSnippet.astro';
import NestedRepositoryProviderSnippet from '~/components/concepts/flutter-bloc/NestedRepositoryProviderSnippet.astro';
import MultiRepositoryProviderSnippet from '~/components/concepts/flutter-bloc/MultiRepositoryProviderSnippet.astro';
import CounterBlocSnippet from '~/components/concepts/flutter-bloc/CounterBlocSnippet.astro';
Expand Down Expand Up @@ -157,6 +158,10 @@ then from `ChildA` we can retrieve the `Repository` instance with:

<RepositoryProviderLookupSnippet />

Repositories that manage resources which must be disposed can do so via the `dispose` callback:

<RepositoryProviderDisposeSnippet />

### MultiRepositoryProvider

**MultiRepositoryProvider** is a Flutter widget that merges multiple `RepositoryProvider` widgets into one.
Expand Down Expand Up @@ -187,19 +192,23 @@ We are going to take a look at how to use `RepositoryProvider` within the contex

<WeatherRepositorySnippet />

Since the app has an explicit dependency on the `WeatherRepository` we inject an instance via constructor. This allows us to inject different instances of `WeatherRepository` based on the build flavor or environment.
In our `main.dart`, we call `runApp` with our `WeatherApp` widget.

<WeatherMainSnippet />

Since we only have one repository in our app, we will inject it into our widget tree via `RepositoryProvider.value`. If you have more than one repository, you can use `MultiRepositoryProvider` to provide multiple repository instances to the subtree.
We will inject our `WeatherRepository` instance into our widget tree via `RepositoryProvider`.

<WeatherAppSnippet />
When instantiating a bloc, we can access the instance of a repository via `context.read` and inject the repository into the bloc via constructor.

In most cases, the root app widget will expose one or more repositories to the subtree via `RepositoryProvider`.
<WeatherAppSnippet />

<WeatherPageSnippet />
:::tip
If you have more than one repository, you can use `MultiRepositoryProvider` to provide multiple repository instances to the subtree.
:::

Now when instantiating a bloc, we can access the instance of a repository via `context.read` and inject the repository into the bloc via constructor.
:::note
Use the `dispose` callback to handle freeing any resources when the `RepositoryProvider` is unmounted.
:::

[flutter_weather_link]: https://github.com/felangel/bloc/blob/master/examples/flutter_weather

Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_bloc_with_stream/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0

dev_dependencies:
bloc_test: ^10.0.0
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_complex_list/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0

dev_dependencies:
bloc_test: ^10.0.0
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_counter/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
bloc: ^9.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0

dev_dependencies:
bloc_test: ^10.0.0
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_dynamic_form/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0

dev_dependencies:
bloc_test: ^10.0.0
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_firebase_login/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ dependencies:
flow_builder: ^0.1.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
font_awesome_flutter: ^10.1.0
form_inputs:
path: packages/form_inputs
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_form_validation/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
formz: ^0.8.0

flutter:
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_infinite_list/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
http: ^1.0.0
stream_transform: ^2.0.0

Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_login/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
formz: ^0.8.0
user_repository:
path: packages/user_repository
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_shopping_cart/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
meta: ^1.0.0

dev_dependencies:
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_timer/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0

dev_dependencies:
bloc_test: ^10.0.0
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_todos/pubspec.lock
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ packages:
path: "../../packages/flutter_bloc"
relative: true
source: path
version: "9.0.0"
version: "9.1.0"
flutter_localizations:
dependency: "direct main"
description: flutter
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_todos/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
flutter_localizations:
sdk: flutter
intl: ^0.19.0
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_weather/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@ void main() async {
? HydratedStorageDirectory.web
: HydratedStorageDirectory((await getTemporaryDirectory()).path),
);
runApp(WeatherApp());
runApp(const WeatherApp());
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,7 @@ class WeatherRepository {
);
}

void dispose() {
_weatherApiClient.close();
}
void dispose() => _weatherApiClient.close();
}

extension on int {
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_weather/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ dependencies:
equatable: ^2.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
google_fonts: ^6.0.0
hydrated_bloc: ^10.0.0
json_annotation: ^4.8.1
Expand Down
2 changes: 1 addition & 1 deletion examples/flutter_wizard/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ dependencies:
flow_builder: ^0.1.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0

flutter:
uses-material-design: true
2 changes: 1 addition & 1 deletion packages/flutter_bloc/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0

flutter:
uses-material-design: true
2 changes: 1 addition & 1 deletion packages/hydrated_bloc/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ environment:
dependencies:
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
hydrated_bloc: ^10.0.0
path_provider: ^2.0.15

Expand Down
2 changes: 1 addition & 1 deletion packages/replay_bloc/example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
bloc: ^9.0.0
flutter:
sdk: flutter
flutter_bloc: ^9.0.0
flutter_bloc: ^9.1.0
replay_bloc: ^0.3.0

flutter:
Expand Down