类型 'int' 不是类型转换 json 中类型 'String' 的子类型 |解码问题

type 'int' is not a subtype of type 'String' in type cast json | decode Problem

我想解码一个大的 JSON 但我得到这个错误。´(类型 'int' 不是类型转换中类型 'String' 的子类型)

return 应该是这样的: 'IdJB' => 2, 'titel' => "lalalala",'datum' => 12052003,等等...

这是我的代码。

Future<Map<String, dynamic>> lol() async {
  final response = await http.post(
      "http://www.bumsbirne.de/2/cool.lol.php", body: {
    "status": "1",
    "nam_ersteller": "Quyre",
    "nummer": 1,
  });
  var data = jsonDecode(response.body);
  
  print(data);
  return data;
}
lol();

这就是控制台

Launching lib/main.dart on iPhone SE (2nd generation) in debug mode...
Running Xcode build...
Xcode build done.                                           13,8s
Waiting for iPhone SE (2nd generation) to report its views...
Debug service listening on ws://127.0.0.1:65295/IdBP__sdzf0=/ws
Syncing files to device iPhone SE (2nd generation)...
[VERBOSE-2:ui_dart_state.cc(171)] Unhandled Exception: type 'int' is not a     subtype of type 'String' in type cast
#0      CastMap.forEach.<anonymous closure>     (dart:_internal/cast.dart:288:25)
#1      _LinkedHashMapMixin.forEach (dart:collection-    patch/compact_hash.dart:377:8)
#2      CastMap.forEach (dart:_internal/cast.dart:287:13)
#3      mapToQuery (package:http/src/utils.dart:17:7)
#4      Request.bodyFields= (package:http/src/request.dart:137:12)
#5      BaseClient._sendUnstreamed (package:http/src/base_client.dart:85:17)
#6      BaseClient.post (package:http/src/base_client.dart:32:7)
#7      post.<anonymous closure> (package:http/http.dart:70:16)
#8      _withClient (package:http/http.dart:166:20)
#9      post (package:http/http.dart:69:5)
#10     _MyHomePageState.initState.lol (package:netzwerk/main.dart:36:30)
#11     _MyHomePageState.initState (package:netzwerk/main.dart:47:8)
#12     StatefulElement._firstBuild         (package:flutter/src/widgets/framework.dart:4702:58)
#13     ComponentElement.mou<…>

感谢您的帮助

您正在将字符串解析为 Int。尝试按照下面的代码。只需将所有数据转换为字符串即可。

Future<Map<String, dynamic>> lol() async {
  final response =
      await http.post("http://www.bumsbirne.de/2/cool.lol.php", body: {
    "status": 1.toString(),
    "nam_ersteller": "Quyre".toString(),
    "nummer": 1.toString(),
  });
  var data = jsonDecode(response.body);

  print(data);
  return data;
}