简体   繁体   中英

Flutter getting _InternalLinkedHashMap error on send data to server

after making two class to create this json structure :

{
  "data": [
    {
      "staff_id": "1111",
      "class_id": "222222",
      "enter_date": "33333333",
      "exit_date": "4444444444444"
    }
  ]
}

i dont have any problem,but when i try to send this structure to server i get error:

getting erorr type '_InternalLinkedHashMap' is not a subtype of type 'Map'

my json structure classes:

@JsonSerializable()
class RestData {
  final List<Data> data;

  RestData(this.data);

  factory RestData.fromJson(Map<String, dynamic> json) => _$RestDataFromJson(json);

  Map<String, dynamic> toJson() => _$RestDataToJson(this);
}

@JsonSerializable()
class Data {
  @JsonKey(name: 'staff_id')
  String staffId;

  @JsonKey(name: 'class_id')
  String classId;

  @JsonKey(name: 'enter_date')
  String enterDate;

  @JsonKey(name: 'exit_date')
  String exitDate;

  Data(this.staffId, this.classId,this.enterDate, this.exitDate);

  factory Data.fromJson(Map<String, dynamic> json) => _$DataFromJson(json);
  Map<String, dynamic> toJson() => _$DataToJson(this);
}

making data and send to server:

List<Data> list = [];
list.add(Data('1111', '222222', '33333333', '4444444444444'));

RestData restData = RestData(list);

print(json.encode(restData.toJson()));

/*result of print: {"data":[{"staff_id":"1111","class_id":"222222","enter_date":"33333333","exit_date":"4444444444444"}]}*/


Map userHeader = {"Content-type": "application/json", "Accept": "application/json"};

final response = await http
    .post('http://sample.com', body: json.encode(restData.toJson()), headers: userHeader)
    .timeout(Duration(seconds: 60));

if (response.statusCode == 200) {
  return true;
} else {
  return false;
}

This is most likely because of the headers - they need to be of type Map<String, String> but you are simply defining them as Map, so Dart chooses the most efficient type of Map for the value that you assign to it, which ends up being that _InternalLinkedHashMap which is for some reason not a subtype of Map<String, String> . I would like to be more specific but searching for '_InternalLinkedHashMap' in the Flutter SDK doesn't bring up any results so I don't know the implementation details.

problem solved

final response = 
await http.post(
    'http://sample.com',
    body:json.encode(restData.toJson()),
    headers: {"Content-type": "application/json"}
).timeout(Duration(seconds: 60));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM