【发布时间】:2021-06-18 07:45:20
【问题描述】:
我已经获取了一个 json 对象并对其进行反序列化,然后也将其返回。
我想在另一个文件中使用它。
我无法分配我在第一步中获得的值。
这里是所有代码...
服务
Future getGeoPoints(String accessToken, String tripId) async {
String requestUrl;
var response = await get(
Uri.parse(requestUrl),
headers: {
'Authorization': "Bearer $accessToken",
},
);
if (response.statusCode == 200) {
Map<String, dynamic> responseBody = json.decode(response.body);
GetGeoPoints geoPoints = GetGeoPoints.fromJson(responseBody);
List listOfGeoPoints = [];
for (var geoPoint in geoPoints.geoPoints) {
listOfGeoPoints.add(
{
'latitude': geoPoint.latitude,
'longitude': geoPoint.longitude,
'timestamp': geoPoint.timeStamp,
},
);
}
// print('List of geo points: ' + '$listOfGeoPoints');
return listOfGeoPoints;
} else {
throw Exception('Failed to load data from server');
}
}
我需要上述值的文件
List routeCoordinates;
Future<void> getValues() async {
getGeoPoints(widget.accessToken, widget.tripId)
.then((value) => routeCoordinates = value);
}
当我运行应用程序时,routeCoordinates 为空,但当我热重载时,它包含该值。
我想在屏幕启动后立即获得这些值。在这里分配值的正确方法是什么?
我也试过这个:
routeCoordinates = getGeoPoints...
它抛出错误..
请帮忙..谢谢..
【问题讨论】: