Reputation: 19444
My FutureBuilder/StreamBuilder
called unnecessary times when navigating using push
. Is it possible to stop without change navigator.push
?
Stream<List<Categorie>> getFromBloc;
@override
void didChangeDependencies() {
getFromBloc = getData();
super.didChangeDependencies();
}
inside state
, outside build
method
Stream<List<Categorie>> getData()=>
_categoryBloc.category;
build method
StreamBuilder(
stream: getFromBloc,//_categoryBloc.category,
builder: (_, AsyncSnapshot<List<Categorie>> snapshot) {
if (snapshot.data == null) {
...
} else {
...
GestureDetector(
child:...,
onTap:(){
Navigator.of(context).pushNamed('/subCategory',arguments: Arguments(postModel: post));
}
}
})
I added debug point inside streamBuilder
. when navigating it's running multiple times.
Upvotes: 0
Views: 481
Reputation: 9903
pushNamed is being called every time getFromBloc stream changes (gets new data). You shouldn't call it anywhere inside build function at all. You might want to do it like this:
getFromBloc.first.then((data) => Navigator.of(context).pushNamed('/subCategory',arguments: Arguments(postModel: post)))
For example, you can do it in initState. Callback will be executed only once after getFromBloc stream gets a new value.
UPD: I got you wrong. Every time you push a new route not just StreamBuilder gets rebuild, but every page on the stack rebuilds to update the back button. There's an issue for that.
This is working as intended. In general, you should assume that all widgets can rebuild at any time, that they don't is mostly just an optimisation.
In particular, routes will rebuild because their navigator state has changed so they might need to update how they draw back buttons and the like.
Upvotes: 1