首页 > 解决方案 > 我的 switch 语句似乎返回了一个值,但并没有一直到它自己的函数

问题描述

我将 switch 语句嵌套在嵌套在函数中的 switch 语句中,该函数应该返回 aa 值以在未来的构建器中使用,但由于某种原因购买使用调试器我注意到从 switch 语句返回的值没有得到所有返回函数语句的方式....我做了很多研究,但似乎没有一个指出我的问题...

_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)
═══

标签: androidflutterdart

解决方案


我不会在Timer.periodic这里使用 a 。如果你想反复更新你的屏幕,你将不得不反复调用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;
      }
 }

推荐阅读