使用 Set State 更新变量数据
Update variables data using Set State
我正在尝试在应用程序恢复时更新这两个变量,变量是分钟和小时。现在,当我恢复应用程序时,值不会更新。
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
dateTimeNow = DateTime.parse('${prefs.getString('startTime')}');
startedDateTime = DateTime.now();
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
if (minutes < 0) {
minutes = 0;
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
print("Inactive");
break;
case AppLifecycleState.paused:
print("Paused");
break;
case AppLifecycleState.resumed:
print('resumed');
setState(() { // trying to updated
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
});
break;
}
}
请在 didChangeAppLifecycleState()
方法的末尾添加 super.didChangeAppLifecycleState(state);
,如下所示:
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
print("Inactive");
break;
case AppLifecycleState.paused:
print("Paused");
break;
case AppLifecycleState.resumed:
print('resumed');
setState(() { // trying to updated
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
});
break;
}
super.didChangeAppLifecycleState(state);
}
并且还覆盖了 dispose 方法:
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
您似乎没有更新 startedDateTime
值。您仅在状态的第一次初始化期间设置它,但您不会在以后更新该值。意思是,即使在使用 didChangeAppLifecycleState
更新您的值之后,startedDateTime
仍然相同,因此 minutes
或 hours
值保持不变。
尝试做这样的事情:
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state); // <-- Add this as well
switch (state) {
<...>
case AppLifecycleState.resumed:
print('resumed');
setState(() {
// trying to updated
startedDateTime = DateTime.now(); // <-- Update the value
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
});
break;
}
}
我正在尝试在应用程序恢复时更新这两个变量,变量是分钟和小时。现在,当我恢复应用程序时,值不会更新。
@override
void initState() {
super.initState();
WidgetsBinding.instance!.addObserver(this);
dateTimeNow = DateTime.parse('${prefs.getString('startTime')}');
startedDateTime = DateTime.now();
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
if (minutes < 0) {
minutes = 0;
}
}
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
print("Inactive");
break;
case AppLifecycleState.paused:
print("Paused");
break;
case AppLifecycleState.resumed:
print('resumed');
setState(() { // trying to updated
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
});
break;
}
}
请在 didChangeAppLifecycleState()
方法的末尾添加 super.didChangeAppLifecycleState(state);
,如下所示:
void didChangeAppLifecycleState(AppLifecycleState state) {
switch (state) {
case AppLifecycleState.inactive:
print("Inactive");
break;
case AppLifecycleState.paused:
print("Paused");
break;
case AppLifecycleState.resumed:
print('resumed');
setState(() { // trying to updated
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
});
break;
}
super.didChangeAppLifecycleState(state);
}
并且还覆盖了 dispose 方法:
void dispose() {
WidgetsBinding.instance!.removeObserver(this);
super.dispose();
}
您似乎没有更新 startedDateTime
值。您仅在状态的第一次初始化期间设置它,但您不会在以后更新该值。意思是,即使在使用 didChangeAppLifecycleState
更新您的值之后,startedDateTime
仍然相同,因此 minutes
或 hours
值保持不变。
尝试做这样的事情:
@override
void didChangeAppLifecycleState(AppLifecycleState state) {
super.didChangeAppLifecycleState(state); // <-- Add this as well
switch (state) {
<...>
case AppLifecycleState.resumed:
print('resumed');
setState(() {
// trying to updated
startedDateTime = DateTime.now(); // <-- Update the value
minutes = startedDateTime.difference(dateTimeNow).inMinutes % 60;
hours = startedDateTime.difference(dateTimeNow).inHours;
});
break;
}
}