'Flutter Streambuilder don´t work fine after Navigator.pop
I have a problem with the Streambuilder.
My app has a Main Page with a Streambuilder stream from Firebase, shown in a Listview.Builder.
After pushing a Button it goes to Page2 with Navigator pushNamed. If I click back on Page2 a setState is called with them on Main Page.
The List and Stream work fine.
But now my problem is if I click a Button on Page2 and Push to Page3 and from there go back with the back icon to Main Page.
I got an error invalid index because the List from Streambuilder is empty. because the setState from Main Page is triggered after pushing from Page2 to Page3 and not at going back from Page3 to Main Page.
Is there a solution to avoid the wrong setState call or a way to refresh the Main Page from Page3 or something like this?
mainPage Streambuilder
Widget build(BuildContext context) {
return RefreshIndicator(
onRefresh: () async {},
child: GestureDetector(
onTapDown: (details) {
_tapPos = details.globalPosition;
},
child: Scaffold(
appBar: BuildAppbar(
title: "Reitschüler",
),
body: StreamBuilder<List<Schueler>>(
stream: IODB().streamSchuelerListFromDB(false),
builder: (context, snapshot) {
if (snapshot.hasData) {
SchuelerList.schuelerList = snapshot.data!;
if (_searchController.text.isEmpty) {
SchuelerList.tempSearchSchuelerList =
SchuelerList.schuelerList;
}
return buildBody();
} else {
return Center(
child: CircularProgressIndicator(),
);
}
}),
floatingActionButton: buildFAB(),
),
));
}
button push from buildBody()
void _choice(String? choice, BuildContext context, String name) {
if (choice != null) {
switch (choice) {
case details:
Navigator.of(context)
.pushNamed(DetailsSchuelerPage.routeName, arguments: indexOfAll)
.then((value) {setState(() {
});});
break;
case delete:
showDialog(
context: context,
builder: (BuildContext context) {
return alert(name, indexOfAll);
},
);
break;
default:
}
}
}
page2
Widget build(BuildContext context) {
final index = ModalRoute.of(context)!.settings.arguments as int;
return Scaffold(
appBar: BuildAppbar(title: "Infos"),
body: StreamBuilder<List<Schueler>>(
stream: IODB().streamSchuelerListFromDB(true),
builder: (context, snapshot) {
if (snapshot.hasData) {
return buildBody(index, snapshot.data!);
} else {
return Center(
child: CircularProgressIndicator(),
);
}
}),
bottomNavigationBar: BottomNavBar(
bottomBarIndex: 0,
index: index,
schueler: true,
),
);
}
go to page 3 with bottomBar
void selectPage(BuildContext context, int index) {
setState(() {
switch (index) {
case 0:
if (widget.schueler!) {
Navigator.of(context).pushReplacementNamed(
DetailsSchuelerPage.routeName,
arguments: widget.index);
} else {
Navigator.of(context).pushReplacementNamed(
ShowDetailsEZSchuelerPage.routeName,
arguments: widget.index);
}
break;
case 1:
if (widget.schueler!) {
Navigator.of(context).pushReplacementNamed(
ShowZahlungenPage.routeName,
arguments: widget.index);
} else {
Navigator.of(context).pushReplacementNamed(
ShowEZZahlungenPage.routeName,
arguments: widget.index);
}
break;
case 2:
if (widget.schueler!) {
Navigator.of(context).pushReplacementNamed(
ShowBemerkungenPage.routeName,
arguments: widget.index);
} else {
Navigator.of(context).pushReplacementNamed(
ShowEZBemerkungenPage.routeName,
arguments: widget.index);
}
break;
default:
if (widget.schueler!) {
Navigator.of(context).pushReplacementNamed(
ShowZahlungenPage.routeName,
arguments: widget.index);
} else {
Navigator.of(context).pushReplacementNamed(
ShowEZZahlungenPage.routeName,
arguments: widget.index);
}
}
});
}
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
