我正在将switch语句嵌套在一个函数中的switch语句中,该函数应该返回一个值,以便在将来的构建器中使用,但由于某种原因,使用调试器时,我注意到switch语句返回的值并不能完全返回函数的return语句……我做了很多研究,但似乎都没有指出我的问题……
_switchFunction(List<String> runningShow) {
Timer.periodic(
Duration(seconds: 10),
(Timer t) {
int timeHour = DateTime.now().hour;
int timeDay = DateTime.now().weekday;
String workingUrl = '';
// day checker switch function
switch (timeDay) {
case (1):
case (2):
case (3):
case (4):
case (5):
// hour checker switch function
switch (timeHour) {
case (0):
case (1):
case (2):
case (3):
case (4):
case (5):
case (6):
case (7):
case (8):
case (9):
return workingUrl = runningShow[0];
case (10):
case (11):
case (12):
case (13):
case (14):
return workingUrl = runningShow[1];
case (16):
case (17):
case (18):
return workingUrl = runningShow[2];
case (19):
case (20):
case (21):
case (22):
case (23):
return workingUrl = runningShow[3];
default:
break;
}
break;
case (6):
if (timeHour >= 14 && timeHour <= 20) {
return workingUrl = runningShow[4];
} else {
return Image.asset(
'assets/default.jpg',
fit: BoxFit.fill,
width: double.infinity,
height: double.infinity,
);
}
break;
case (7):
return Image.asset(
'assets/default.jpg',
fit: BoxFit.fill,
width: double.infinity,
height: double.infinity,
);
break;
default:
}
print(workingUrl);
return workingUrl;
},
);}在评论之后,我仍然收到这个错误。
Restarted application in 2,320ms.
I/flutter ( 5052): AsyncSnapshot<List<dynamic>>(ConnectionState.waiting, null, null)
I/flutter ( 5052): AsyncSnapshot<List<dynamic>>(ConnectionState.done, [https://i.postimg.cc/NjZGz6fS/Studio-Screens-Wake-01.png, https://i.postimg.cc/Gtbmjbhp/Studio-Screens-Mid-01.png, https://i.postimg.cc/4x0nTCw8/Studio-Screens-Drive-01.png, https://i.postimg.cc/RZhq8C16/Studio-Screens-Maloko-01.png], null)
I/flutter ( 5052): Fetched Urls are:[https://i.postimg.cc/NjZGz6fS/Studio-Screens-Wake-01.png, https://i.postimg.cc/Gtbmjbhp/Studio-Screens-Mid-01.png, https://i.postimg.cc/4x0nTCw8/Studio-Screens-Drive-01.png, https://i.postimg.cc/RZhq8C16/Studio-Screens-Maloko-01.png]
I/flutter ( 5052): this is null
════════ Exception caught by image resource service ════════════════════════════
The following ArgumentError was thrown resolving an image codec:
Invalid argument(s): No host specified in URI file:///null
When the exception was thrown, this was the stack
#0 _HttpClient._openUrl (dart:_http/http_impl.dart:2187:9)
#1 _HttpClient.getUrl (dart:_http/http_impl.dart:2118:48)
#2 NetworkImage._loadAsync
package:flutter/…/painting/_network_image_io.dart:84
<asynchronous suspension>
#3 NetworkImage.load
package:flutter/…/painting/_network_image_io.dart:48
...
Image provider: NetworkImage("null", scale: 1.0)
Image key: NetworkImage("null", scale: 1.0)
═══发布于 2019-10-07 17:26:06
我不会在这里使用Timer.periodic。如果你想反复更新你的屏幕,你将不得不反复调用setState() {}或者类似的方法。如果你像下面这样简化你的代码,很明显你不会返回任何东西:
_switchFunction(List<String> runningShow) {
Timer.periodic(
Duration(seconds: 10),
(Timer t) {
int timeHour = DateTime.now().hour;
int timeDay = DateTime.now().weekday;
String workingUrl = '';
switch(timeDay) {
case 1:
workingURL = 'foo';
return workingURL;
break;
default:
break;
}
}); // End of Timer.periodic
// We could do quite a lot of stuff here.
// for examply, we could return just something random:
return 'foo';
}但是,使用以下代码段,它将正常工作:
_switchFunction(List<String> runningShow) {
int timeHour = DateTime.now().hour;
int timeDay = DateTime.now().weekday;
String workingUrl = '';
switch(timeDay) {
case 1:
workingURL = 'foo';
return workingURL;
break;
default:
break;
}
}https://stackoverflow.com/questions/58266236
复制相似问题