Flutter Heroes
25.8K subscribers
272 photos
2 videos
31.1K links
Download Telegram
New post on /r/flutterdev subreddit:

Do StreamBuilders Require InitialData?
StreamBuilders seem to require an InitialData to be set in order to work. However, it should be the case that setting the initial value can be handled within a BLoC. Unfortunately, when I try to initialize data within a BLoC and skip setting initialData on the StreamBuilder, it returns null and throws and error on the StreamBuilder. In the examples I've seen, StreamBuilder is always set with an initial value. I'm curious as to whether this is mandatory.​void main() => runApp(BlocProvider<AppBloc>(bloc: AppBloc(), child: App()));class AppBloc implements BlocBase {
AppProvider appProvider = AppProvider();StreamController<String> appController = StreamController();
Stream get getPage => appController.stream;AppBloc() {//I'm trying to initialize the BLoC data here.
updatePage(appProvider._page);
}void updatePage(String page) {
appProvider.updatePage(page);
appController.sink.add(appProvider._page);
}void dispose() {
appController.close();
}
}class AppProvider {
String _page = window.defaultRouteName ?? "";void updatePage(String page) {
_page = page;
}
}class App extends StatelessWidget {
@override
Widget build(BuildContext context) {
final AppBloc appBloc = BlocProvider.of<AppBloc>(context);
return StreamBuilder(
stream: appBloc.getPage,
initialData: appBloc.appProvider._page,
builder: (context, snapshot) {
print(snapshot.data);
return _widgetForRoute(snapshot.data);
});
}
}​With the code above, if I don't set the initial data in the StreamBuilder, I get the following error.​snapshot.data = nullThe following ArgumentError was thrown building StreamBuilder<dynamic>(dirty, state:_StreamBuilderBaseState<dynamic, AsyncSnapshot<dynamic>>#5409d):Invalid argument(s)​Then the snapshot gets the data and everything works.​Is there a way to avoid the error? I'm thinking that the initialization order is incorrect or I'm not initialize the default BLoC value correctly.

May 11, 2019 at 02:21AM by gamelaunchplatform
http://bit.ly/2vSzhMh