Search code examples
sharedpreferencesflutter

Flutter: Access Stored Sharedpreference value from Other Pages


I'm storing a value in a page called page1.dart. I want to access the stored value from page2.dart or page3.dart. How can I achieve this?


Solution

  • I have stored a string in 1st screen,then retrieved the stored value from the second screen by the following code

    PAGE 1:

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    import 'package:trial/class2.dart';
    
    void main() => runApp(MyApp());
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: MyHomePage(),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      String _email = '';
    
      @override
      void initState() {
        super.initState();
        _loadCounter();
      }
    
      _loadCounter() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          _email = (prefs.getString('email') ?? '');
        });
      }
    
      _incrementCounter() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          _email = "[email protected]";
        });
        prefs.setString('email', _email);
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text("title"),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  '$_email',
                ),
                new RaisedButton(
                  child: new Text("next2"),
                  onPressed: () {
                    Navigator.push(context,
                        new MaterialPageRoute(builder: (context) => new Next()));
                  },
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    PAGE 2:

    import 'package:flutter/material.dart';
    import 'package:shared_preferences/shared_preferences.dart';
    String _email='';
    class Next extends StatefulWidget {
      @override
      _NextState createState() => _NextState();
    }
    
    class _NextState extends State<Next> {
        @override
      void initState() {
        super.initState();
        _loadCounter();
      }
    
      _loadCounter() async {
        SharedPreferences prefs = await SharedPreferences.getInstance();
        setState(() {
          _email = (prefs.getString('email')??'');
        });
      }
      @override
      Widget build(BuildContext context) {
    
        return Container(
          child: new Text("read value from sharepreference is:"+_email),
        );
      }
    }