flutter check if app is closed code example

Example: resume last page flutter killed app

void main() {
  runApp(
    RootRestorationScope(
      restorationId: 'root',
      child: MaterialApp(home: HomePage()),
    ),
  );
}

class HomePage extends StatefulWidget {
  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> with RestorationMixin {
  final RestorableInt _counter = RestorableInt(0);

  @override
  String get restorationId => 'HomePage';

  @override
  void restoreState(RestorationBucket oldBucket, bool initialRestore) {
    registerForRestoration(_counter, 'counter');
  }

  @override
  void dispose() {
    _counter.dispose();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          child: Text('${_counter.value}'),
          onPressed: () => setState(() => ++_counter.value),
        ),
      ),
    );
  }
}

Tags:

Misc Example