flutter : how to persist a List using shared preferences?
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
add a comment |
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
add a comment |
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
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
android dart flutter
edited Nov 24 '18 at 9:05
ViralCode
asked Nov 24 '18 at 7:38
ViralCodeViralCode
194
194
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
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
worked perfectly, just usedList<num>
andnum.parse()
instead , thanks.
– ViralCode
Nov 24 '18 at 13:35
add a comment |
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
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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
worked perfectly, just usedList<num>
andnum.parse()
instead , thanks.
– ViralCode
Nov 24 '18 at 13:35
add a comment |
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
worked perfectly, just usedList<num>
andnum.parse()
instead , thanks.
– ViralCode
Nov 24 '18 at 13:35
add a comment |
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
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
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 usedList<num>
andnum.parse()
instead , thanks.
– ViralCode
Nov 24 '18 at 13:35
add a comment |
worked perfectly, just usedList<num>
andnum.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
add a comment |
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
add a comment |
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
add a comment |
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
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
answered Nov 24 '18 at 8:03
Rahul MahadikRahul Mahadik
2,3012925
2,3012925
add a comment |
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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