-
-
Notifications
You must be signed in to change notification settings - Fork 3.4k
Update BlocProvider to automatically dispose blocs #344
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
Conversation
bfdec42 to
5380896
Compare
Codecov Report
@@ Coverage Diff @@
## master #344 +/- ##
=====================================
Coverage 100% 100%
=====================================
Files 9 9
Lines 164 181 +17
=====================================
+ Hits 164 181 +17
Continue to review full report at Codecov.
|
|
Hi, in case I want to access Bloc via |
|
@anticafe can you provide your use-case? With these changes our intention is to highly recommend creating/providing a bloc using the |
|
With this change, if I still need a stateful widget can I create my blocs in the initState like I use to, or does all bloc creation have to use this new BlocProvider format? |
|
@warriorCoder with this change you will no longer need to make |
|
@anticafe #347 should address your concerns 👍 |
|
Hi @felangel I don't remember the reason correctly but in my own project, I don't dispose Bloc until the MyApp.dispose() is called. It seems if dispose a Bloc then next time enter this screen again, I cannot re-initialize it. |
|
@anticafe I have updated all of the docs/tutorials to use the new |
|
Hello @felangel , i have tried to update my project to the newest flutter_bloc version 16.0. I made all changed that are required, but now my App crashes when i try to access the Blocs within a ```BlocListenerTree``. Error Message: App Architecture: This worked fine until version 16.0, so is this not longer supported or is this one of the edge cases where are you working on? King regards Max |
|
Hi @Knupper 👋 You need to move the void main() {
runApp(
BlocProviderTree(
blocProviders: [
BlocProvider<BlocA>(
builder: (BuildContext context) =>
BlocA(FirestoreProvider()),
dispose: (BuildContext context, BlocA bloc) => bloc.dispose(),
),
BlocProvider<BlocB>(
builder: (BuildContext context) =>
GroupBloc(FirestoreProvider(), CloudProvider()),
dispose: (BuildContext context, BlocB bloc) => bloc.dispose(),
),
],
child: MyApp(),
),
);
}Then you can refactor class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocListenerTree(
blocListeners: [
BlocListener<BlocAEvent, BlocAState>(
bloc: BlocProvider.of<BlocA>(context),
listener: (BuildContext context, BlocAState state) {
// Do sth.
},
),
BlocListener<BlocBEvent, BlocBState>(
bloc: BlocProvider.of<BlocB>(context),
listener: (BuildContext context, BlocBStatestate) {
// Do sth.
},
),
],
child: BlocBuilder(
bloc: BlocProvider.of<BlocB>(context),
builder: (BuildContext context, BlocBState state) {
return MaterialApp(
theme: state.theme,
debugShowCheckedModeBanner: BuildMode().isDebugMode,
title: 'MyApp',
home: MainPage(),
);
},
),
);
}
}Hope that helps 👍 |
|
Thank you for your reply @felangel . This was my workaround, but i was not sure if this is the best solution for it. Keep going with the plugin, it helps me a lot with a good architecture. :) |
Status
READY
Breaking Changes
YES
Description
Update
BlocProviderto handle initializing and disposing the bloc to eliminate the need to create aStatefulWidgetjust to manage a bloc (#84).This PR allows us to go from:
To this:
Todos
Impact to Remaining Code Base