I'm facing a problem with my flutter app when i need to jump from the first to the third Tab. i'll explain better below:
I'm building a screen using flutter when i've a TabBar with 3 Tabs and below a TabBarView to populate this tabs.
something like that:
Widget _buildReportAppbar() {
return AppBar(
backgroundColor: AppColors.colorPrimary,
elevation: 5,
leading: ...
title: ...
actions: ...
bottom: PreferredSize(
preferredSize: Size.fromHeight(58.0),
child: Padding(
padding: EdgeInsets.only(bottom: 10),
child: TabBar(
labelColor: AppColors.colorPrimary,
labelPadding: EdgeInsets.only(left: 8, right: 8),
labelStyle: TextStyle(fontSize: 14, fontWeight: FontWeight.w500),
unselectedLabelColor: Colors.white,
unselectedLabelStyle:
TextStyle(fontSize: 14, fontWeight: FontWeight.normal),
indicatorSize: TabBarIndicatorSize.label,
isScrollable: true,
indicatorWeight: 0,
indicator: BoxDecoration(
borderRadius: BorderRadius.circular(12),
color: Colors.white,
),
controller: _tabController,
tabs: _tabs,
),
),
),
);
}
Widget _buildReportBody() {
return TabBarView(
controller: _tabController,
children: _provideTabScreenList(),
);
}
My tab controller is working like a charm and inside each TabBarView I've an statefulWidget that build my report screens. Each StatefulWidget has an api call that bring me the report information and with the SetState(){} method i put the informations on the screen.
List<Widget> _provideTabScreenList() {
_tabScreenList.clear();
_tabScreenList.add(PeriodResumeReport(filterData: currentFilter));
_tabScreenList.add(SaleStatisticReport(filterData: currentFilter));
_tabScreenList.add(SalePerDayReport(filterData: currentFilter));
return _tabScreenList;
}
class PeriodResumeReport extends StatefulWidget {
final _periodResumeReportState = _PeriodResumeReportState();
final SelectedFilter filterData;
PeriodResumeReport({Key key, @required this.filterData}) : super(key: key);
@override
_PeriodResumeReportState createState() => _periodResumeReportState;
//My tabController has a listener that detect the position change and notify the child.
void isVisible() {
_periodResumeReportState.isVisible();
}
}
//I'm using the AutomaticKeepAliveClientMixin to keep the state when i move between the
childs of the TabBarView
class _PeriodResumeReportState extends State<PeriodResumeReport>
with AutomaticKeepAliveClientMixin<PeriodResumeReport> {
var _loadingData = false;
var _apiErrorMessage = "";
var _hasResponseFromApi = false;
var _response = ...api response;
@override
void setState(fn) {
if (mounted) {
super.setState(fn);
}
}
//Load first data when the screen visible for the first time.
@override
void initState() {
super.initState();
_reloadData();
}
@override
Widget build(BuildContext context) {
super.build(context);
return ListView(
children: <Widget>[
_loadingData ? LinearProgressIndicator() : Container(),
_apiErrorMessage.isNotEmpty
? Padding(...)
: Container(),
_hasResponseFromApi ? _buildTotalOfSalesContainer() : Container(),
_hasResponseFromApi ? _buildComparativeChart() : Container(),
],
);
}
Widget _buildTotalOfSalesContainer() {
return ...;
}
Widget _buildComparativeChart() {
return ...;
}
//reload data if the user return to the screen
void isVisible() {
_reloadData();
}
Future<void> _reloadData() async {
//show ProgressBar and clear apiError
setState(() {
_loadingData = true;
_apiErrorMessage = "";
});
try {
var response = .... api call ....
....
.... handle api response
....
setState(() {
_response = response;
_loadingData = false;
_hasResponseFromApi = true;
});
}
@override
bool get wantKeepAlive => true;
}
And with this code everything is working fine. But with one problem, if i swipe to the first tab to second and after to third tab, is all ok. The problem happen if I'm in the first tab and click to move to third tab instead to pass to the second before. Doing this, the second tab crash with this error:
Exception has occurred.
FlutterError (setState() called after dispose(): _SaleStatisticReportState#8c846(lifecycle
state: defunct)
This error happens if you call setState() on a State object for a widget that no longer
appears in the widget tree (e.g., whose parent widget no longer includes the widget in its.
build). This error can occur when code calls setState() from a timer or an animation
callback.
The preferred solution is to cancel the timer or stop listening to the animation in the
dispose() callback. Another solution is to check the "mounted" property of this object
before calling setState() to ensure the object is still in the tree.
This error might indicate a memory leak if setState() is being called because another object
is retaining a reference to this State object after it has been removed from the tree. To
avoid memory leaks, consider breaking the reference to this object during dispose().)
in this line:
@override
void setState(fn) {
if(mounted){
super.setState(fn);
}
}
I don't know what's the problem, maybe the action of swipe from first tab directly to third tab run the isVisible method and start api call on the second tab but, as I'm on the third tab, the setState(){} on the second crash. How I solve this?