Flutter: How to keep the same Screen before closed it?

Viewed 45

I want to keep the view or session of some screens. For example, if I close the app, I can then go back to the last open screen.

I think this is possible using SharedPreferences, but I just know that SharedPreferences is for user data only. I would like to know some alternatives, thanks!

import 'package:flutter/material.dart';

class KeepScreen extends StatefulWidget {
  CKeepScreena({Key? key}) : super(key: key);

  @override
  State<CKeepScreen> createState() => _CKeepScreenState();
}

class _KeepScreenState extends State<KeepScreen> {
  @override
  Widget build(BuildContext context) {
    return Container();
  }
}
1 Answers

Using Shared Preferences like you said, you can do something like this:

  1. Every time that you navigate to a screen that you want to come back, you save this information on Shared prefs.
  2. When you init your app, you read the shared preferences to check the last page saved, and navigate again to this page. You can do this on your main() or inside another method called on app startup.
Related