Flutter RootRestorationScope not restoring last root

809

use restorablePush rather than push and give every route an restorationId

Routes added with the classic imperative API (push, pushNamed, and friends) can never restore their state. A Route added with the restorable imperative API (restorablePush, restorablePushNamed, and all other imperative methods with "restorable" in their name) restores its state if all routes below it up to and including the first Page-based route below it are restored. If there is no Page-based route below it, it only restores its state if all routes below it restore theirs.

https://api.flutter.dev/flutter/widgets/Navigator-class.html

Share:
809
Salim Lachdhaf
Author by

Salim Lachdhaf

Updated on December 26, 2022

Comments

  • Salim Lachdhaf
    Salim Lachdhaf over 1 year

    I was using this plugin https://pub.dev/packages/last_state, but when i update to flutter 1.22 a new feature came out RootRestorationScope that seems could replace that external plugin, but after testing it and following some tutos (tuto1, tuto2) i noticed that it restore the state but it does not restore the route ! am i missing something ? there is an example:

    import 'package:flutter/material.dart';
    
    void main() => runApp(
      RootRestorationScope( // Register a restoration scope for the entire app!
        restorationId: 'root',
        child: MyApp(),
      ),
    );
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: DummyPage(),
        );
      }
    }
    
    class DummyPage extends StatelessWidget {
      static const route = "/intermediate";
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('Another page'),
          ),
          body: RaisedButton(
            child: Text("Onwards"),
            onPressed: () {
              Navigator.of(context, rootNavigator: false).push(
                MaterialPageRoute<bool>(
                  fullscreenDialog: true,
                  builder: (BuildContext context) => HomePage(),
                ),
              );
            },
          ),
        );
      }
    }
    
    class HomePage extends StatefulWidget {
      @override
      _HomePageState createState() => _HomePageState();
    }
    
    // Our state should be mixed-in with RestorationMixin
    class _HomePageState extends State<HomePage> with RestorationMixin {
    
      // For each state, we need to use a restorable property
      final RestorableInt _index = RestorableInt(0);
    
      @override
      Widget build(BuildContext context) {
        return RestorationScope(
          restorationId: 'tess',
          child: Scaffold(
            body: Center(child: Text('Index is ${_index.value}')),
            bottomNavigationBar: BottomNavigationBar(
              currentIndex: _index.value,
              onTap: (i) => setState(() => _index.value = i),
              items: <BottomNavigationBarItem>[
                BottomNavigationBarItem(
                    icon: Icon(Icons.home),
                    label: 'Home'
                ),
                BottomNavigationBarItem(
                    icon: Icon(Icons.notifications),
                    label: 'Notifications'
                ),
                BottomNavigationBarItem(
                    icon: Icon(Icons.settings),
                    label: 'Settings'
                ),
              ],
            ),
          ),
        );
      }
    
      @override
      // The restoration bucket id for this page,
      // let's give it the name of our page!
      String get restorationId => 'home_page';
    
      @override
      void restoreState(RestorationBucket oldBucket, bool initialRestore) {
        // Register our property to be saved every time it changes,
        // and to be restored every time our app is killed by the OS!
        registerForRestoration(_index, 'nav_bar_index');
      }
    }