函数调用未返回 null 但仍给出 "null" 返回错误

The function call is not returning null but still giving "null" to be returned error

我正在将旧版本的 flutter 代码迁移到具有空安全功能的最新版本。

在函数调用中我收到错误“正文可能正常完成,导致 'null' 被 returned,但 return 类型可能是不可空的类型”。我已将我的代码包含在 try catch 块中,并在 catch 块中添加了 rethrow 语句以防止空异常。

这是我的代码。

    Future<Map<String, dynamic>> fetchTimeline(http.Client client) async {
        try {
          print('INVOICE URL: ${globals.ursl.getURL(URLS.GETINVOICEURL)}');

          Response response;
          Dio dio = new Dio();

          response = await dio.get(globals.ursl.getURL(URLS.GETINVOICEURL));
          print('INVOICE GET RESPONSE: $response');
          if (response.statusCode == 200) {
            Map mapobject = (json.decode(response.toString()));
            var succes = mapobject['success'];
            if (succes == 1) {
              if (mapobject['Invoice'][0]['address'] == null ||
                  mapobject['Invoice'][0]['address'] == '') {
                address = '';
              } else {
                address = mapobject['Invoice'][0]['address'];
              }

              if (mapobject['Invoice'][0]['contact'] == null ||
                  mapobject['Invoice'][0]['contact'] == '')
                phone = '';
              else
                phone = mapobject['Invoice'][0]['contact'];

              if (mapobject['Invoice'][0]['restaurant_name'] == null ||
                  mapobject['Invoice'][0]['restaurant_name'] == '') {
                name = ' ';
              } else {
                name = mapobject['Invoice'][0]['restaurant_name'];
              }
              logo = mapobject['Invoice'][0]['logo'];
              globals.invoiceData = mapobject['Invoice'][0];
              startTime();
              return mapobject['Invoice'][0];
            } else {
              return {};
            }
          }
        } catch (error) {
          client.close();

          print("CONNECTION CLOSED: $error");
          rethrow;
        }
      }

我在 catch 块中添加了重新抛出,但仍然存在错误。 任何人都可以帮助我。

谢谢

很难看到所有嵌套的 if 语句,但您不会在每个分支中返回 Map<String, dynamic>。此条件 if (response.statusCode == 200) { ... } 没有相应的 else 分支,因此如果 statusCode200 以外的某个值,则您不会返回任何内容(这意味着您隐式返回 null 在这种情况下)。