I am using the flutter_bloc package.
I have a page with a tabbar. The page creates a bloc and provides it to the widgets, which are used as tab content.
class _MyScreenState extends State<MyScreen> {
MyBloc _myBloc;
...
Widget _buildTabBar() {
...
var children = [
_buildFirstTab()
_buildSecondTab(),
];
return WillPopScope(
onWillPop: _onWillPop,
child: DefaultTabController(
length: children.length,
child: Scaffold(
appBar: PreferredSize(
preferredSize: Size.fromHeight(75.0),
child: AppBar(
bottom: TabBar(isScrollable: true, tabs: tabs),
)),
body: TabBarView(
children: children,
),
),
),
);
}
..
}
Widget _buildSecondTab() {
if (_myBloc == null) {
_myBloc = MyBloc(<..initialdata..>);
}
_myBloc.add(LoadServerDataEvent());
return BlocProvider<MyBloc>(create: (_) => _myBloc, child: SecondTab());
}
The widget on the second tab gets the bloc from the BlocProvider and uses it.
class _SecondTabState extends State<SecondTab> {
MyBloc _myBloc;
@override
void initState() {
_myBloc = BlocProvider.of<MyBloc>(context);
super.initState();
}
My problem is that the block is automatically closed when I switch between the tabs.
This is kind of strange, because the block is still used on the page.