flutter : how to persist a List using shared preferences?












0















how to persist a List like List<num> = [2.5, 5, 7.5, 10] using SharedPreferences please ?



EDIT : how to convert stored data as String or List<String> to List ?










share|improve this question





























    0















    how to persist a List like List<num> = [2.5, 5, 7.5, 10] using SharedPreferences please ?



    EDIT : how to convert stored data as String or List<String> to List ?










    share|improve this question



























      0












      0








      0


      1






      how to persist a List like List<num> = [2.5, 5, 7.5, 10] using SharedPreferences please ?



      EDIT : how to convert stored data as String or List<String> to List ?










      share|improve this question
















      how to persist a List like List<num> = [2.5, 5, 7.5, 10] using SharedPreferences please ?



      EDIT : how to convert stored data as String or List<String> to List ?







      android dart flutter






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 24 '18 at 9:05







      ViralCode

















      asked Nov 24 '18 at 7:38









      ViralCodeViralCode

      194




      194
























          2 Answers
          2






          active

          oldest

          votes


















          1














          First, you need to convert your list of integers to a List of Strings,
          then you save it in shared preferences.



          You do the opposite when loading.



          This is a complete example:



          import 'package:flutter/material.dart';
          import 'package:shared_preferences/shared_preferences.dart';

          void main() {
          runApp(new MaterialApp(
          home: new Scaffold(
          body: new Center(
          child: new RaisedButton(
          onPressed: _save,
          child: new Text('Save my list of int'),
          ),
          ),
          ),
          ));
          }

          _save() async {

          List<int> myListOfIntegers = [1,2,3,4];
          List<String> myListOfStrings= myListOfIntegers.map((i)=>i.toString()).toList();

          SharedPreferences prefs = await SharedPreferences.getInstance();
          List<String> myList = (prefs.getStringList('mylist') ?? List<String>()) ;
          List<int> myOriginaList = myList.map((i)=> int.parse(i)).toList();
          print('Your list $myOriginaList');
          await prefs.setStringList('mylist', myListOfStrings);
          }


          Don't forget to add this to your pup spec.yaml file:



          shared_preferences: ^0.4.3





          share|improve this answer


























          • worked perfectly, just used List<num> and num.parse() instead , thanks.

            – ViralCode
            Nov 24 '18 at 13:35





















          0














          As per documentation, there is no way to store list of numbers. But you implement it using:



           setStringList(String key, List<String> value) → Future<bool>


          Saves a list of strings value to persistent storage in the background.



          Implementation:



          Future<bool> setStringList(String key, List<String> value) =>
          _setValue('StringList', key, value);


          Another alternate way is like store whole list as a String.



          Full documentation can be found in the Flutter packages repository.



          Hope this will help you






          share|improve this answer























            Your Answer






            StackExchange.ifUsing("editor", function () {
            StackExchange.using("externalEditor", function () {
            StackExchange.using("snippets", function () {
            StackExchange.snippets.init();
            });
            });
            }, "code-snippets");

            StackExchange.ready(function() {
            var channelOptions = {
            tags: "".split(" "),
            id: "1"
            };
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function() {
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled) {
            StackExchange.using("snippets", function() {
            createEditor();
            });
            }
            else {
            createEditor();
            }
            });

            function createEditor() {
            StackExchange.prepareEditor({
            heartbeatType: 'answer',
            autoActivateHeartbeat: false,
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader: {
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            },
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            });


            }
            });














            draft saved

            draft discarded


















            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456163%2fflutter-how-to-persist-a-listnum-using-shared-preferences%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            2 Answers
            2






            active

            oldest

            votes








            2 Answers
            2






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes









            1














            First, you need to convert your list of integers to a List of Strings,
            then you save it in shared preferences.



            You do the opposite when loading.



            This is a complete example:



            import 'package:flutter/material.dart';
            import 'package:shared_preferences/shared_preferences.dart';

            void main() {
            runApp(new MaterialApp(
            home: new Scaffold(
            body: new Center(
            child: new RaisedButton(
            onPressed: _save,
            child: new Text('Save my list of int'),
            ),
            ),
            ),
            ));
            }

            _save() async {

            List<int> myListOfIntegers = [1,2,3,4];
            List<String> myListOfStrings= myListOfIntegers.map((i)=>i.toString()).toList();

            SharedPreferences prefs = await SharedPreferences.getInstance();
            List<String> myList = (prefs.getStringList('mylist') ?? List<String>()) ;
            List<int> myOriginaList = myList.map((i)=> int.parse(i)).toList();
            print('Your list $myOriginaList');
            await prefs.setStringList('mylist', myListOfStrings);
            }


            Don't forget to add this to your pup spec.yaml file:



            shared_preferences: ^0.4.3





            share|improve this answer


























            • worked perfectly, just used List<num> and num.parse() instead , thanks.

              – ViralCode
              Nov 24 '18 at 13:35


















            1














            First, you need to convert your list of integers to a List of Strings,
            then you save it in shared preferences.



            You do the opposite when loading.



            This is a complete example:



            import 'package:flutter/material.dart';
            import 'package:shared_preferences/shared_preferences.dart';

            void main() {
            runApp(new MaterialApp(
            home: new Scaffold(
            body: new Center(
            child: new RaisedButton(
            onPressed: _save,
            child: new Text('Save my list of int'),
            ),
            ),
            ),
            ));
            }

            _save() async {

            List<int> myListOfIntegers = [1,2,3,4];
            List<String> myListOfStrings= myListOfIntegers.map((i)=>i.toString()).toList();

            SharedPreferences prefs = await SharedPreferences.getInstance();
            List<String> myList = (prefs.getStringList('mylist') ?? List<String>()) ;
            List<int> myOriginaList = myList.map((i)=> int.parse(i)).toList();
            print('Your list $myOriginaList');
            await prefs.setStringList('mylist', myListOfStrings);
            }


            Don't forget to add this to your pup spec.yaml file:



            shared_preferences: ^0.4.3





            share|improve this answer


























            • worked perfectly, just used List<num> and num.parse() instead , thanks.

              – ViralCode
              Nov 24 '18 at 13:35
















            1












            1








            1







            First, you need to convert your list of integers to a List of Strings,
            then you save it in shared preferences.



            You do the opposite when loading.



            This is a complete example:



            import 'package:flutter/material.dart';
            import 'package:shared_preferences/shared_preferences.dart';

            void main() {
            runApp(new MaterialApp(
            home: new Scaffold(
            body: new Center(
            child: new RaisedButton(
            onPressed: _save,
            child: new Text('Save my list of int'),
            ),
            ),
            ),
            ));
            }

            _save() async {

            List<int> myListOfIntegers = [1,2,3,4];
            List<String> myListOfStrings= myListOfIntegers.map((i)=>i.toString()).toList();

            SharedPreferences prefs = await SharedPreferences.getInstance();
            List<String> myList = (prefs.getStringList('mylist') ?? List<String>()) ;
            List<int> myOriginaList = myList.map((i)=> int.parse(i)).toList();
            print('Your list $myOriginaList');
            await prefs.setStringList('mylist', myListOfStrings);
            }


            Don't forget to add this to your pup spec.yaml file:



            shared_preferences: ^0.4.3





            share|improve this answer















            First, you need to convert your list of integers to a List of Strings,
            then you save it in shared preferences.



            You do the opposite when loading.



            This is a complete example:



            import 'package:flutter/material.dart';
            import 'package:shared_preferences/shared_preferences.dart';

            void main() {
            runApp(new MaterialApp(
            home: new Scaffold(
            body: new Center(
            child: new RaisedButton(
            onPressed: _save,
            child: new Text('Save my list of int'),
            ),
            ),
            ),
            ));
            }

            _save() async {

            List<int> myListOfIntegers = [1,2,3,4];
            List<String> myListOfStrings= myListOfIntegers.map((i)=>i.toString()).toList();

            SharedPreferences prefs = await SharedPreferences.getInstance();
            List<String> myList = (prefs.getStringList('mylist') ?? List<String>()) ;
            List<int> myOriginaList = myList.map((i)=> int.parse(i)).toList();
            print('Your list $myOriginaList');
            await prefs.setStringList('mylist', myListOfStrings);
            }


            Don't forget to add this to your pup spec.yaml file:



            shared_preferences: ^0.4.3






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 27 '18 at 3:09









            Pang

            6,9301664102




            6,9301664102










            answered Nov 24 '18 at 9:02









            Saed NabilSaed Nabil

            1,21828




            1,21828













            • worked perfectly, just used List<num> and num.parse() instead , thanks.

              – ViralCode
              Nov 24 '18 at 13:35





















            • worked perfectly, just used List<num> and num.parse() instead , thanks.

              – ViralCode
              Nov 24 '18 at 13:35



















            worked perfectly, just used List<num> and num.parse() instead , thanks.

            – ViralCode
            Nov 24 '18 at 13:35







            worked perfectly, just used List<num> and num.parse() instead , thanks.

            – ViralCode
            Nov 24 '18 at 13:35















            0














            As per documentation, there is no way to store list of numbers. But you implement it using:



             setStringList(String key, List<String> value) → Future<bool>


            Saves a list of strings value to persistent storage in the background.



            Implementation:



            Future<bool> setStringList(String key, List<String> value) =>
            _setValue('StringList', key, value);


            Another alternate way is like store whole list as a String.



            Full documentation can be found in the Flutter packages repository.



            Hope this will help you






            share|improve this answer




























              0














              As per documentation, there is no way to store list of numbers. But you implement it using:



               setStringList(String key, List<String> value) → Future<bool>


              Saves a list of strings value to persistent storage in the background.



              Implementation:



              Future<bool> setStringList(String key, List<String> value) =>
              _setValue('StringList', key, value);


              Another alternate way is like store whole list as a String.



              Full documentation can be found in the Flutter packages repository.



              Hope this will help you






              share|improve this answer


























                0












                0








                0







                As per documentation, there is no way to store list of numbers. But you implement it using:



                 setStringList(String key, List<String> value) → Future<bool>


                Saves a list of strings value to persistent storage in the background.



                Implementation:



                Future<bool> setStringList(String key, List<String> value) =>
                _setValue('StringList', key, value);


                Another alternate way is like store whole list as a String.



                Full documentation can be found in the Flutter packages repository.



                Hope this will help you






                share|improve this answer













                As per documentation, there is no way to store list of numbers. But you implement it using:



                 setStringList(String key, List<String> value) → Future<bool>


                Saves a list of strings value to persistent storage in the background.



                Implementation:



                Future<bool> setStringList(String key, List<String> value) =>
                _setValue('StringList', key, value);


                Another alternate way is like store whole list as a String.



                Full documentation can be found in the Flutter packages repository.



                Hope this will help you







                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 24 '18 at 8:03









                Rahul MahadikRahul Mahadik

                2,3012925




                2,3012925






























                    draft saved

                    draft discarded




















































                    Thanks for contributing an answer to Stack Overflow!


                    • Please be sure to answer the question. Provide details and share your research!

                    But avoid



                    • Asking for help, clarification, or responding to other answers.

                    • Making statements based on opinion; back them up with references or personal experience.


                    To learn more, see our tips on writing great answers.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53456163%2fflutter-how-to-persist-a-listnum-using-shared-preferences%23new-answer', 'question_page');
                    }
                    );

                    Post as a guest















                    Required, but never shown





















































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown

































                    Required, but never shown














                    Required, but never shown












                    Required, but never shown







                    Required, but never shown







                    Popular posts from this blog

                    Wiesbaden

                    Marschland

                    Dieringhausen