I have a Flutter application with some kinda weird functionality, but it's required one. The application has BottomNavigationBar with pages wrapped in IndexedStack, so that if you go to other page of this IndexedStack, the pages do not reload and stay persistent.
class _MyHomePageState extends State<MyHomePage> {
//...
@override
Widget build(BuildContext context) {
return Scaffold(
body: IndexedStack(
index: selectedIndex,
children: bottomBarPages(),
),
bottomNavigationBar: BottomNavigationBar(
items: navItems,
currentIndex: selectedIndex,
onTap: _onNavItemTapped,
),
);
}
}
One of these pages is a ProfilePage, where you can choose between list of users. I use ChangeNotifier that looks like this:
class MainUserInfo extends ChangeNotifier {
List<UserInfo> _users;
UserInfo _chosenUser;
//... some get/set/etc methods here
}
One of other BottomNavigationBar pages TheOtherPage depends on the chosen user (_chosenUser), on initState it loads data from the Net:
class TheOtherPage extends StatefulWidget {
//...
bool isLoading = true;
@override
void initState() {
super.initState();
getAccountData();
}
void getAccountData() async {
//some fetch happening here
setState(() {
model = response.data;
isLoading = false;
});
}
}
The thing is that after field _chosenUser changes, I need TheOtherPage to load data of chosen user from the Net. But I don't know how to handle it. If I use context.read or context.select it surely tracks changing _chosenUser, but doesn't fetch data of newly chosen one:
var mainUserInfo = context.read<MainUserInfo>();
//or
var chosenUser = context.select<MainUserInfo, UserInfo>((userInfo) => userInfo.chosenUser);
And I don't wanna save data needed for TheOtherPage in UserInfo object, because there are just too much data, and loading so much data for every user in MainUserInfo._users is not an option. Also I would to be able to keep track of loading state, because using "bool Isloading" I can show some fancy placeholders while required data is being fetched.
So the general question is how to make TheOtherPage fetch data on changing field of ChangeNotifier on ProfilePage. Maybe my whole approach is bad and I don't need to use Provider and ChangeNotifier here, I would like to see any suggestions. Thanks