I have a two pages, in one page, i open Hive box but when I navigate to second page, the dispose() method runs and closes the Hive box. but the problem is, when i click on ‘Back’ button, the initState doesnt rerun on the first page, so I couldn’t open the box again through initState.
here is the code on First page,
@override
initState() {
super.initState();
Hive.openBox<boxModel>('customTable');
}
@override
void dispose() {
Hive.close();
super.dispose();
}
Here is the back in appbar in second page,
AppBar(
leadingWidth: 100,
leading: IconButton(
onPressed: () => Navigator.of(context).pop(),
icon: Icon(
Icons.arrow_back,
color: AppTheme.colors.greyFontColor,
),
),
backgroundColor: AppTheme.colors.appBarColor,
elevation: 0,
iconTheme: IconThemeData(color: AppTheme.colors.greyFontColor),)
so is there a way to re run to the initState upon the back button pressed on second page.
Thanks for any help..
2
Answers
try (context as Element).reassemble(); try this snippet refresh and build widgets again in current screen flutter.
Navigator.pushReplacement(context, MaterialPageRoute(builder: (context) =>PreviousPage() ,)); for previous using navigator.
I don’t think that dispose runs when you push a new route, at least not as long as there is a back button to go back, but anyways if you just want to open the box again you can add this to your
Navigator.push