flutter - FormatException:使用 OSM 映射意外结束输入(在字符 1 处)
问题描述
我正在尝试从我的 API 中获取所有标记(同时大约 3k 点)并将它们放在 OSM 地图上。我解析 JSON 并将必要的列表放在 http-request 中。我还在小部件内使用了一个循环,并尝试将我需要的所有点都放在循环中,并使它们出现在 OSM 地图上。但现在我得到了这个错误:
I/flutter (25693): FormatException: Unexpected end of input (at character 1)
I/flutter (25693):
I/flutter (25693): ^
我的模型:
List<Stop> stopFromJson(String str) => List<Stop>.from(json.decode(str).map((x) => Stop.fromJson(x)));
String stopToJson(List<Stop> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Stop {
Stop({
this.stLat,
this.stLong,
});
final double stLat;
final double stLong;
Http请求是:
Future<List<Stop>> fetchStops() async {
String username = '';
String password = '';
String basicAuth =
'Basic ' + base64Encode(utf8.encode('$username:$password'));
print(basicAuth);
final response = await http.get(
Uri.parse(
link),
headers: <String, String>{'authorization': basicAuth});
var jsonResponse = convert.jsonDecode(response.body) as List;
return jsonResponse.map((e) => Stop.fromJson(e)).toList();
小部件内部的循环,我尝试在其中迭代所有 ppints 并将它们放在地图上:
@override
void initState() {
futureStops = fetchStops();
super.initState();
}
List<Stop> listStops;
Future<List<Stop>> futureStops;
List<Marker> allMarkers = [];
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
body: FutureBuilder(
future: futureStops,
builder: (BuildContext context, AsyncSnapshot<dynamic> snapshot) {
if (snapshot.hasData) {
listStops = snapshot.data;
listStops.forEach((Stops) {
allMarkers = listStops.map(
(e) => Marker(
width: 1,
height: 1,
point: latLong.LatLng(e.stLat, e.stLat),
builder: (_) => Icon(
Icons.person_pin,
color: Colors.green,
)),
).toList(growable: true);;
});
我不明白到底出了什么问题,因为我似乎正确解析了所有内容并且一切都应该工作,但我仍然收到此错误。它可以与什么连接?我到底哪里错了?我将衷心感谢您的帮助。
解决这个问题 在我看来,错误就在这里:
factory Stop.fromJson(Map<String, dynamic> json) => Stop(
stLat: json["st_lat"],
stLong: json["st_long"],
解决方案
修复它:问题是关于解析:应该是这样的:stLat: json["st_lat"].toDouble(), stLong: json["st_long"].toDouble()
,我很确定我做的一切都是正确的,但不知何故它没有按我想要的方式解析。现在都修好了。
对于任何将从我的问题中获取循环代码的人:它不起作用。我将尝试解决它并在此处粘贴答案。
推荐阅读
- python - 如何使用 PyTorch 并行化 CNN 对图像的评估
- r - 将视觉元素(图标)添加到 ggpplot 标签
- ckeditor - ckeditor 自动在视觉上调整拖放图像的大小
- java - Azure Active Directory 的 Spring Security Oauth2
- sql - 在 SQL Server 中将 Varbinary 转换为 PDF
- dkim - 我可以只为一台特定的服务器配置 DKIM 吗?
- extjs - EXTJS:Ext.widget 和 Ext.create 有什么区别?
- deployment - 无法使用 apache2 mod_wsgi 设置 odoo
- java - 部署 Springboot Web 服务 Heroku。配置 Dyno 形成时出现错误 [无法访问 jarfile server.port]
- angular - Angular 7+ - 注入和子类