gpt4 book ai didi

Flutter 'List' 不是类型转换中类型 'String' 的子类型

转载 作者:IT王子 更新时间:2023-10-29 06:39:09 24 4
gpt4 key购买 nike

在 flutter 中,我应该为服务器端应用程序制作这个 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)向服务器发送数据:

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

对 JsonSerializable 的更新:

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

最佳答案

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) 解析你的 json

关于Flutter 'List<Data>' 不是类型转换中类型 'String' 的子类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57362472/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com