I have learned that it brings problems when initating a future- or streambuilder inside the build of a widget since it will lead to unwanted fetching of data.
To do this you pass the stream outside of the build-method. But this leads to another problem. In my case I have to get an id-argument from the last widget but that can only be reached inside the build. I have searched the internet for explanations on how to solve this but I cant find an easy and clean explanation on the best way to do this. A good solution that initialize the future with the build outside of the build.
My material app:
MaterialApp(
routes: appRoutes,
);
My routes table:
var appRoutes = {
Screen1.routeName: (context) => Screen1(),
Screen2.routeName: (context) => Screen2(),
};
How I send the argument from screen1:
onTap: () {
Navigator.of(context).pushNamed(
Screen2.routeName,
arguments: id,
);
},
screen2: (which doesnt work this way)
class Screen2 extends StatefulWidget {
static const routeName = '/screen2';
@override
State<Screen2> createState() => _Screen2State();
}
class _Screen2State extends State<Screen2> {
@override
final String id = ModalRoute.of(context)!.settings.arguments as String; <====== Impossible
final _stream = FirestoreService().getData(id);
Widget build(BuildContext context) {
return FutureBuilder<Map>(
future: _stream,
builder: (context, snapshot) {
if (snapshot.hasData) {
print('succes');
} else {
print('Fail');
}
});
}
}