generic_bloc_provider
Flutter 앱용 일반적인 BloC 제공자입니다. 이 패키지는 BloC 제공자를 작성할 때 반복되는 코드를 피하는 데 도움이 됩니다.
일반적인 BloC 제공자 구현
{"sdk":"flutter"}{"sdk":"flutter"}아래는 영문 원문 스냅샷입니다. 최신 내용은 GitHub에서 확인하세요.
A generic BloC Provider for your Flutter apps.
All your BloCs must extend the Bloc abstract class. This means that all of them should have a dispose method that will be executed whenever the life of the BloC comes to an end.
In this method, you should take care of closing all the sinks and resources.
Example of how to use it:
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return BlocProvider(
bloc: AppBloc(),
child: MaterialApp(
title: 'Yo Sleep',
home: MainPage(),
initialRoute: 'main',
routes: {
'main': (context) => MainPage(),
},
),
);
}
}
class MainPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
final appBloc = BlocProvider.of<AppBloc>(context);
return Scaffold(
body: SafeArea(
child: Column(
children: <Widget>[
Header(),
RecordList(appBloc),
],
),
),
);
}
}
The BlocProvider class depends on InheritedWidget. Whenever you want to get your BloC, you can decide wether to attach the context of your widget to the InheritedWidget or not.
In order to control this behavior, the static method of has an optional boolean argument (which is true by default) which determines wether your context will be attached or not.
Basically, if you don't provide it or you just set it to true, dependOnInheritedWidgetOfExactType will be used. If you set it to false then getElementForInheritedWidgetOfExactType will be used instead.
If you want to change the way updateShouldNotify behaves you have the option to provide a custom anonymous function to the BlocProvider constructor.
You will notice that there's an argument called updateShouldNotifyOverride which accepts a function receiving a BloC and the internal InheritedWidget:
bool Function(T bloc, _BlocProvider oldWidget);
This is the default implementation of the updateShouldNotify method:
@override
bool updateShouldNotify(_BlocProvider oldWidget) =>
updateShouldNotifyOverride != null
? updateShouldNotifyOverride!(bloc, oldWidget)
: oldWidget.bloc != bloc;