http - http.get 和 Null 类型响应的问题

标签 http flutter dart get

我正在与奥地利公共(public)交通 API 的 OGD 合作开展一个项目,该项目只需要一个带有“stopID”的 getter 请求。您还可以连接多个 stopID。 这意味着如果您尝试此链接,您将收到包含 stopID 的所有数据的响应:

http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=2913&stopId=2918&stopId=2921&stopId=3387&stopId=4133&stopId=4134

现在这是我的问题: 编号为 4133 的 stopID(尝试 http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=4133 )在坐标中具有 NULL 值。通常我只会做一些 NULL 检查,但有一些我从未见过的奇怪行为。我已经调试到错误所在的位置。它位于 HTTP.get 请求并显示:

I/flutter (29464): ERROR: Invalid argument(s) (input): Must not be null 

但是如果我此时还没有得到回复怎么可能呢? 它尝试请求并在构建响应时中断某处。

这是它的请求代码(finalURl 是上面的 URL):

    final Response response = await http.get(finalUrl);
    if (response.statusCode == 200) {
      final encode = jsonDecode(response.body);
}

该错误甚至发生在 if 语句和解析 json 字符串之前。 所以在它获得正确的响应对象之前。如果您现在问我如何知道为什么会因为坐标字段中的 NULL 类型而发生这种情况,我已经尝试过不带 ID 4133 的请求并且工作正常。如果我只使用第二个请求链接(只有 ID 4133),它会抛出错误。 有人知道那里出了什么问题吗?这绝对是 Dart/Flutter 的问题,我错过了什么吗?

最佳答案

我不知道你是如何导入http包的。如果您使用“http”别名来调用 get(),请确保按如下方式导入,

import 'package:http/http.dart' as http;

并且还使用包中的 Response 类作为

http.Response;

我尝试从给定的网址获取数据,它对我有用。

...
import 'package:http/http.dart' as http;
...

Future<void> fetchData() async {

    const url = "http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=4133";

    final http.Response response = await http.get(url);
    final data = json.decode(response.body) as Map<String, dynamic>;
    print(data);

}

或者,您可以跳过类(class)并让 Dart 推断响应类型,如下所示:

...
import 'package:http/http.dart' as http;
...

Future<void> fetchData() async {

    const url = "http://www.wienerlinien.at/ogd_realtime/monitor?&stopId=4133";

    final response = await http.get(url);
    final data = json.decode(response.body) as Map<String, dynamic>;
    print(data);

}

调试控制台:

I/flutter (20609): {data: {monitors: [{locationStop: {type: Feature, geometry: {type: Point, coordinates: [null, null]}, properties: {name: 60200949, title: Oberlaa, municipality: Wien, municipalityId: 90001, type: stop, coordName: WGS84, gate: 2, attributes: {rbl: 4133}}}, lines: [{name: U1, towards: NICHT EINSTEIGEN ! NO DEPARTURE PLATFORM, direction: R, platform: 2, richtungsId: 2, barrierFree: true, realtimeSupported: true, trafficjam: false, departures: {departure: [{departureTime: {}, vehicle: {name: U1, towards: NICHT EINSTEIGEN ! NO DEPARTURE PLATFORM, direction: R, richtungsId: 2, barrierFree: false, realtimeSupported: true, trafficjam: false, type: ptMetro, attributes: {}, linienId: 301}}]}, type: ptMetro, lineId: 301}], attributes: {}}]}, message: {value: OK, messageCode: 1, serverTime: 2020-07-19T15:55:30.000+0200}}

然后您可以对数据进行空值检查。还可以尝试将代码放入 try catch block 中,无论您在哪里调用 fetchData() 函数,以正确处理错误。

关于http - http.get 和 Null 类型响应的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62979282/

相关文章:

flutter - GridView的子级定义为类成员时不会重绘

javascript - 编译为 javascript 是否会阻止 dart 添加弱引用?

http - HTTP 下载是如何工作的?

javascript - Angular $http 不显示所有标题

将项目滚动出屏幕时,Flutter ListView 小部件会重置图标

flutter - SingleChildScrollView不保持滚动位置

javascript - JavaScript 可以设置哪些 HTTP header ?

javascript - 在 NodeJS 中处理多个 http 调用的最佳方式

flutter - 如何使用 Navigator.of(context).pop() 在一秒后隐藏 alertDialog

dart - DART-调整div元素大小的事件