在扑朔迷离中,我应该为服务器端应用程序制作此json结构:
{
"data": [
{
"staff_id": "barcode1",
"class_id": "session_id1",
"class_name": "session_name1",
"enter_date": "enter_date1",
"Exit_date": "exit_date1",
"username": "username1"
},
{
"staff_id": "barcode2",
"class_id": "session_id2",
"class_name": "session_name2",
"enter_date": "enter_date2",
"Exit_date": "exit_date2",
"username": "username2"
}
]
}
class RestData{
final List<Data> data;
RestData({@required this.data});
Map<String, dynamic> toJson() => {
'data':data
};
}
class Data {
final String staff_id;
final String class_id;
final String class_name;
final String enter_date;
final String exit_date;
final String username;
Data({this.staff_id,this.class_id,this.class_name,this.enter_date,this.exit_date,this.username});
Data.fromJson(Map<String, dynamic> json)
: staff_id = json['staff_id'],
class_id = json['class_id'],
class_name = json['class_name'],
enter_date = json['enter_date'],
exit_date = json['exit_date'],
username = json['username'];
Map<String, dynamic> toJson() {
var map = new Map<String, dynamic>();
map["staff_id"] = staff_id;
map["class_id"] = class_id;
map["class_name"] = class_name;
map["enter_date"] = enter_date;
map["exit_date"] = exit_date;
map["username"] = username;
return map;
}
}
//--------------------------------------
List<Data> list = [];
for (int i = 0; i < barcodes.length; i++) {
list.add(Data(staff_id: 'aaa', class_id: '1111', class_name: '2222', enter_date: '3333', exit_date: '444444', username: '5555'));
}
restData = {RestData}
data = {_GrowableList} size = 1
0 = {Data}
staff_id = "aaa"
class_id = "1111"
class_name = "2222"
enter_date = "3333"
exit_date = "444444"
username = "5555"
现在!当我尝试将此结果发送到服务器时,出现此错误:
List<Data> is not a subtype of type 'String' in type cast
我的班级将数据发送到服务器:
class SendInformation{
Future<bool> sendDataToServer(List<Data> list) async{
RestData restData = RestData(data: list);
final response = await http.post('http://sample.com',body:restData.toJson()).timeout(Duration(seconds: 60));
if(response.statusCode ==200){
return true;
}else{
return false;
}
}
}
我不确定,但是我认为问题出在这行代码中:
restData.toJson()
part 'rest_data.g.dart';
@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);
}
part 'data.g.dart';
@JsonSerializable()
class Data {
@JsonKey(name: 'staff_id')
String staffId;
@JsonKey(name: 'class_id')
String classId;
@JsonKey(name: 'class_name')
String className;
@JsonKey(name: 'enter_date')
String enterDate;
@JsonKey(name: 'exit_date')
String exitDate;
@JsonKey(name: 'username')
String username;
Data(this.staffId, this.classId, this.className, this.enterDate, this.exitDate, this.username);
factory Data.fromJson(Map<String, dynamic> json) => _$DataFromJson(json);
Map<String, dynamic> toJson() => _$DataToJson(this);
}
答案 0 :(得分:2)
import 'dart:async';
import 'dart:convert';
import 'dart:io';
import 'package:http/http.dart' as http;
class SendInformation{
Future<bool> sendDataToServer(List<Data> list) async{
RestData restData = RestData(data: list);
final response = await http.post('http://sample.com',body:json.encode(restData.toJson())).timeout(Duration(seconds: 60));
if(response.statusCode ==200){
return true;
}else{
return false;
}
}
}
使用json.encode(Your_JSON_String)