skip to Main Content

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


  1. 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.

    Login or Signup to reply.
  2. 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

    Navigator.of(context)
    .push(MaterialPageRoute(
      builder: (context) =>  SecondPage(),
    )).then((_) {
      Hive.openBox<boxModel>('customTable');
    });
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search