I’m watching a Flutter app development tutorial on Udemy, and the instructor frequently uses Streams or Futures to load data from the database and then deploy it in the app.
I was quite surprised by this approach because I primarily use Providers combined with didChangeDependencies to fetch my data. This allows me to set certain conditions (example below) and seems to give me more control over when and how to load my data (for example, in this case, I decide to load my FindsRequests only when the user presses a button). It also allows me to easily reuse data from one page to another.
@override
void didChangeDependencies() {
super.didChangeDependencies();
final appProvider = Provider.of<AppProvider>(context, listen: false);
final userProvider = Provider.of<UserProvider>(context, listen: false);
if (appProvider.indexFriendsFindsSegmentedButton == 1) {
userProvider.getFindsAccounts();
} else {
userProvider.getFriendsAccounts();
}
}
@override
void didChangeDependencies() {
Provider.of<UserProvider>(context).getUser(context);
super.didChangeDependencies();
}
IconButton(
onPressed: () {
userProvider.getFindsRequestsAccounts();
_scaffoldKey.currentState?.openEndDrawer();
},
icon: Icon(Icons.mail_rounded),
)
However, to achieve this, I need to create a considerable number of Provider variables to temporarily store my data.
So, I wanted to know if one approach is recommended over the other and if, in this case, I should consider other solutions to optimize my application.