I Have a BottomNavigationBar, and there are multiple pages within it. One of my pages is a Statefulwidget, and within this widget, there is a Future builder. However, I would rather not rebuild my FutureBuilder even after switching tabs.
class CurrenPlanDetail extends StatefulWidget {
static const routeName = 'current-plan-detail';
@override
State<CurrenPlanDetail> createState() => _CurrenPlanDetailState();
}
class _CurrenPlanDetailState extends State<CurrenPlanDetail> {
Future<int>? _myFuture;
@override
void didChangeDependencies() {
if (_isInit) {
Provider.of<Plans>(context).getServerPlan(_errorDialog);
}
_isInit = false;
super.didChangeDependencies();
}
@override
void initState() {
_myFuture = returnData();
super.initState();
}
Future<int> returnData() async {
return await Provider.of<Users>(context, listen: false).subRemainDays();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
//elevation: 0,
title: Text('title'),
//backgroundColor: Colors.transparent,
),
//backgroundColor: kFrostyWhite,
body: Container(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
CurrentPlanBox(),
FutureBuilder(
future: _myfuture,
builder: (context, snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Text('loading....');
}
return Text(snapshot.data.toString());
}),
Container(
margin: EdgeInsets.only(top: 10.h),
child: PlanSlider(),
),
],
),
),
);
}
}
I attempted to declare my future as a variable within the initSatate method, but it doesn't work Every time I switch tabs, my future gets reloaded.