how to store the list of string in shared Preference ? and print that list in widget?

278

For save a string list data:

SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setStringList('YOUR-DATA-KEY', yourList);

For get string list data:

SharedPreferences prefs = await SharedPreferences.getInstance();
var yourList = prefs.getStringList('YOUR-DATA-KEY');

And also you can read Flutter's SharedPreferences documentation at: https://pub.dev/packages/shared_preferences

Share:
278
Author by

Jahidul Islam

Enthusiastic

Updated on December 31, 2022

Comments

  • Jahidul Islam less than a minute

    i m new to flutter and i m unable to store the data in shared Preference actually i m working on small project and now i m stuck to store the data in shared Preference for later use so anyone have any idea please help me ...! Thankyou. here is my code :

        import 'package:flutter/material.dart';
        import 'home.dart';
        class Result extends StatelessWidget {
        final String selectedLevel;
        final String timeTakenInmin;
        Result({Key key, @required this.selectedLevel,@required this.timeTakenInmin}) : 
        super(key: key);
        @override
        Widget build(BuildContext context) {
          var size = MediaQuery.of(context).size;
          return WillPopScope(
            onWillPop: () =>Future.value(false),
            child: Scaffold(
              body: Center(
                  child: Column(
                    children: [
                      Expanded(flex:2,child: LayoutBuilder(
                        builder: (context,constraints) => Container(
                          color: Color(0xFF317C84),
                          child: Center(
                            child: Text('Your Result',style: TextStyle(
                                color: Colors.black54,fontWeight: FontWeight.bold,fontSize: 30
                            ),),
                          ),
                        ),
                      )),
                      Expanded(flex:1,child: LayoutBuilder(
                      builder: (context,constraints) => Container(
                        decoration: BoxDecoration(
                            image: DecorationImage(
                              image: AssetImage("assets/reporticon.png",),scale: 4,
                            )
                        ),
                      ),
                    )),
                    Expanded(flex:1,child: LayoutBuilder(
                      builder: (context,constraints) => Container(height: size.height*.3,
                          child: Column(
                              children: [
                                DataTable(
                                  columns: [
                                    DataColumn(label: Text('Level Selected')),
                                    DataColumn(label: Text('Time Taken')),
                                  ],
                                  rows: [
                                    DataRow(cells: [
                                      DataCell(Text(selectedLevel)),
                                      DataCell(Text(timeTakenInmin)),
                                    ])
                                  ],
                                ),
                              ])),
                    )),
                    Expanded(flex:1,child: LayoutBuilder(
                      builder: (context,constraints) => Center(
                        child: ElevatedButton(style: ButtonStyle(backgroundColor: MaterialStateProperty.all(Color(0xFF317C84))),
                          onPressed: (){
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) {
                                  return Home();
                                },
                              ),
                            );
                          },
                          child: Text("Exit"),
                        ),
                      ),
                    )),
                  ],
                )
            ),
          ),
        );}}
    

    In this image I want to get the list of prev recorded data in list