我找到了这个,我正在尝试重新创建它。
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);
}
我在导入为选项的外部文件中有一个名为timerValuesList的列表。现在我不明白SharedPreferences pref = ...之后的行,为什么还有另一个字符串列表?那是为了在不存在的情况下创建列表吗? 这是我的文件。我也会负担。
现在我已经尝试了。我注意到这一切都很好,但是当我在某些小部件中使用值时,例如timerValuesList [0],它显示的是30.0,而不是我在其他函数中修改的值,然后将其保存到sharedpref。但是,如果我随后编辑例如+1的值,则它不是从显示的30.0开始,而是从我上次编辑并更新的值开始。因此,这是正确的值,但是直到我更新屏幕后才向我显示。你知道为什么吗?该问题仅在我首次启动该应用程序时出现,之后再没问题,这就是为什么我虽然将loadSharedPreferences放在initState中但仍然出现。
List<double> timersValuesList = [30.0, 60.0, 90.0, 120.0, 150.0, 180.0];
void saveSharedPreferences() async {
// from List of double to a List of String
List<String> convertedTimerValues = timersValuesList.map((i) => i.toString()).toList();
// getting the instance of sharedPreferences as Object prefs
SharedPreferences prefs = await SharedPreferences.getInstance();
// taking my List as key of "mylist", if it doesn't exist create empty List, this is List of String
List<String> myList = (prefs.getStringList('mylist') ?? List<String>());
//saving the List of String as key"mylist"
await prefs.setStringList('mylist', convertedTimerValues);
}
void loadSharedPreferences() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> myList = (prefs.getStringList('mylist') ?? List<String>());
List<double> myOriginalList = myList.map((i)=> double.parse(i)).toList();
timersValuesList = myOriginalList;
// print('Your list $myOriginalList');
}
答案 0 :(得分:1)
不能100%确定您不了解的内容,
SharedPreferences prefs = await SharedPreferences.getInstance();
很明显,上面是在获取SharedPreferences实例。
List<String> myList = (prefs.getStringList('mylist') ?? List<String>());
上面说的是获取myList和“ ??”如果为null,则创建一个空列表。
答案 1 :(得分:1)
在
Public Function StringFixLeft(AStr As Variant, SLen As Integer) As String
If (IsNull(AStr)) Then AStr = ""
If (Len(AStr) = 0) Then AStr = " "
StringFixLeft = UCase(Format(Left(AStr, SLen), "!" & String(SLen, "@")))
End Function
Public Function StringFixRight(AStr As Variant, SLen As Integer) As String
If (IsNull(AStr)) Then AStr = ""
If (Len(AStr) = 0) Then AStr = " "
StringFixRight = UCase(Format(Left(AStr, SLen), String(SLen, "@")))
End Function
// Here is what I wrote in PHP.
function StringFixRight($AStr, $SLen) {
if ($AStr === NULL) {$AStr = "";}
if (strlen($AStr) = 0) {$AStr = "";}
$StringFixRight = strtoupper(substr($AStr,0,$SLen));
return $StringFixRight;
}
function StringFixLeft($AStr, $SLen) {
if ($AStr === NULL) {$AStr = "";}
if (strlen($AStr) = 0) {$AStr = "";}
$StringFixRight = strtoupper(substr($AStr,0,$SLen));
return $StringFixRight;
}
// basically the line I cannot figure out is the last line in the function
StringFixLeft = UCase(Format(Left(AStr, SLen), "!" & String(SLen, "@")))
StringFixRight = UCase(Format(Left(AStr, SLen), String(SLen, "@")))
// need to convert these to PHP
中,您需要输入SharedPreferences
作为密钥来保存您的价值。
获取实例后
String
下一行使用键SharedPreferences prefs = await SharedPreferences.getInstance();
检索列表(它是String的列表)。
'mylist'
在下一行中,代码通过解析List<String> myList = (prefs.getStringList('mylist') ?? List<String>());
myList
您可以
如何保存:
List<int> myOriginaList = myList.map((i)=> int.parse(i)).toList();
您会发现其他功能来保存其他类型的价值。
要检索:
SharedPreferences prefs = await SharedPreferences.getInstance();
await prefs.setStringList('your_string_list_key', yourStringList);