Hole Closer
Hole Closer

Reputation: 13

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

I want to decode a large JSON but i get this error.´(type 'int' is not a subtype of type 'String' in type cast)

The return should look like this: 'IdJB' => 2, 'titel' => "lalalala",'datum' => 12052003,etc...

This is my Code.

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();

Thats the Console

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<…>

Thanks for any help

Upvotes: 1

Views: 1014

Answers (1)

Salim Murshed
Salim Murshed

Reputation: 1451

You are parsing the string as an Int. Try to follow the code below. Just convert all data into a string.

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;
}

Upvotes: 1

Related Questions