我正在尝试在应用加载时加载保存在sharedPreferences中的值。但是,它始终为我提供了首次构建时声明为double的静态列表[0,0,0,0,0,0,0]。每当我触摸设置状态的东西时,它就会更改为正确的值。我什至不需要声明该静态列表,因为dart在问我。如果其中没有任何内容,我想要默认列表,否则,我想要sharedpreferences保存的列表。这是我的代码。
@override initState() {
super.initState();
callLoad();
}
callLoad() async{
List<double> list = await loadTimers();
setState((){
timersList = list;
});
}
Future<List<double>> loadTimers() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> myList = prefs.getStringList('TimerList');
List<double> defaultTimersList = [30.0, 60.0, 90.0, 120.0, 150.0, 180.0];
List<double> savedTimerList;
if (myList == null ){
return defaultTimersList;
} else{
savedTimerList = myList.map((i)=> double.parse(i)).toList();
return savedTimerList;
}
}
static List<double> timersList = [0,0,0,0,0,0];
double timeRemaining1 = timersList[0];
double timeRemaining2 = timersList[1];
double timeRemaining3 = timersList[2];
double timeRemaining4 = timersList[3];
double timeRemaining5 = timersList[4];
double timeRemaining6 = timersList[5];
我也尝试过这种方法,但是还是一样。
@override initState() {
super.initState();
loadTimers();
}
loadTimers() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> myList = prefs.getStringList('TimerList');
List<double> defaultTimersList = [30.0, 60.0, 90.0, 120.0, 150.0, 180.0];
List<double> savedTimerList;
if (myList == null ){
setState(() {
timersList = defaultTimersList;
});
} else{
savedTimerList = myList.map((i)=> double.parse(i)).toList();
setState(() {
timersList = savedTimerList;
});
}
}
static List<double> timersList = [0,0,0,0,0,0];
double timeRemaining1 = timersList[0];
double timeRemaining2 = timersList[1];
double timeRemaining3 = timersList[2];
double timeRemaining4 = timersList[3];
double timeRemaining5 = timersList[4];
double timeRemaining6 = timersList[5];
谢谢
使用显示问题的小应用程序进行编辑。
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: MyHomePage(title: 'Flutter Demo chokuns page'),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => _MyHomePageState();
}
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
@override initState() {
super.initState();
callLoad();
}
callLoad() async{
List<double> list = await loadTimers();
setState((){
timersList = list;
});
}
Future<List<double>> loadTimers() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
List<String> myList = prefs.getStringList('TimerList');
List<double> defaultTimersList = [30.0, 60.0, 90.0, 120.0, 150.0, 180.0];
List<double> savedTimerList;
if (myList == null ){
return defaultTimersList;
} else{
savedTimerList = myList.map((i)=> double.parse(i)).toList();
return savedTimerList;
}
}
static List<double> timersList = [0,0,0,0,0,0];
double timeRemaining1 = timersList[0];
double timeRemaining2 = timersList[1];
double timeRemaining3 = timersList[2];
double timeRemaining4 = timersList[3];
double timeRemaining5 = timersList[4];
double timeRemaining6 = timersList[5];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button \nthis many times:',
textAlign: TextAlign.center,
),
Text(
'$timeRemaining1', //showing here $timersList it shows correct values
style: Theme.of(context).textTheme.display1,
),
],
),
),
);
}
}
答案 0 :(得分:1)
将passwd
更改为:
LD_PRELOAD
答案 1 :(得分:0)
基本上,Dart通过引用传递对象。
因此,您的想法是正确的。但是,我将解释为什么它不起作用。
由List<double> list = await loadTimers();
引起,因为它是新对象。
下面的代码显示TrueTrue
。
但是,如果将//isSameList = identical(timersList, timersList2);
更改为isSameList = identical(timersList, timersList2);
,则会显示FalseTrue
。您必须重新启动它。
class _MyHomePageState extends State<MyHomePage> {
int _counter = 0;
SharedPreferences prefs;
static List<double> timersList = [0,0,0,0,0,0];
double timeRemaining1;
List<double> timersList2;
bool isSameList;
bool isSameValue;
@override initState() {
super.initState();
initValues();
callLoad();
}
void initValues()
{
timeRemaining1 = timersList[0];
timersList2 = timersList;
isSameList = identical(timersList, timersList2);
isSameValue = identical(timersList[0], timeRemaining1);
}
Future<void> callLoad() async{
prefs = await SharedPreferences.getInstance();
List<double> list = await loadTimers();
setState((){
timersList = list;
//isSameList = identical(timersList, timersList2);
});
}
List<double> loadTimers() {
List<String> myList = prefs.getStringList('TimerList');
List<double> defaultTimersList = [30.0, 60.0, 90.0, 120.0, 150.0, 180.0];
List<double> savedTimerList;
if (myList == null ){
return defaultTimersList;
} else{
savedTimerList = myList.map((i)=> double.parse(i)).toList();
return savedTimerList;
}
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text(
'You have pushed the button \nthis many times:',
textAlign: TextAlign.center,
),
Text(
'$isSameList'+'$isSameValue', //showing here $timersList it shows correct values
style: Theme.of(context).textTheme.display1,
),
],
),
),
);
}
最后,我知道如何将pass by reference
用于列表。
timersList.setAll(0, list);
它很好地显示了结果。但是,timeRemaining1
仍然无法显示,因为它无法保留reference
。因此,我认为您可以按照上述方式使用List对象来处理它。