FormatException: Unexpected character (at character 3) I/flu



我想返回API数据时,我做jsonDecode,但我得到这个错误:FormatException:意外字符(at character 3)I/flutter (32137): [{Id: 0, SourceId: 0, ServiceId: 11, CategoryId: 5, Category: Valuation, De…]

我的反应。数据返回一个有效的json,但当我想解码,我得到那个错误,可以请任何人帮助。

我的代码如下:

Future<Autogenerated?>? signInData() async {
final prefs = await SharedPreferences.getInstance();
final String? token = prefs.getString('token');
try {
Response response = await _dio.post('$_baseUrl/api/gateway',
data: {
"ClientPackageId": "0cdd231a-d7ad-4a68-a934-d373affb5100",
"PlatformId": "ios",
"ClientUserId": "AhmedOmar",
"VinNumber": VINumber
},
options: Options(
headers: {
"Content-Type": "application/json;charset=UTF-8",
"Charset": 'utf-8',
"Authorization": "Bearer $token",
},
));
print("data is here");
print(response.data.toString());
print(response.statusCode);
if (response.statusCode == 200) {
print("decoded");
print(Autogenerated.fromJson(jsonDecode(response.data.toString())));
return Autogenerated.fromJson(jsonDecode(response.data.toString()));
} else if (response.statusCode == 500) {
// call your refresh token api here and save it in shared preference
print(response.statusCode);
await getToken();
signInData();
} else {
throw Exception('Failed to load data');
}
} catch (e) {
print(e);
}
//  return null;
}

My Autogenerated model

class Autogenerated {
int? id;
int? sourceId;
int? serviceId;
int? categoryId;
String? category;
String? description;
int? serviceResponsePropertyId;
int? mappingId;
bool? isVisible;
int? packageRequestId;
int? sortOrder;
Value? value;
Autogenerated(
{this.id,
this.sourceId,
this.serviceId,
this.categoryId,
this.category,
this.description,
this.serviceResponsePropertyId,
this.mappingId,
this.isVisible,
this.packageRequestId,
this.sortOrder,
this.value});
Autogenerated.fromJson(Map<String, dynamic> json) {
id = json['Id'];
sourceId = json['SourceId'];
serviceId = json['ServiceId'];
categoryId = json['CategoryId'];
category = json['Category'];
description = json['Description'];
serviceResponsePropertyId = json['ServiceResponsePropertyId'];
mappingId = json['MappingId'];
isVisible = json['IsVisible'];
packageRequestId = json['PackageRequestId'];
sortOrder = json['SortOrder'];
value = json['Value'] != null ? new Value.fromJson(json['Value']) : null;
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['Id'] = this.id;
data['SourceId'] = this.sourceId;
data['ServiceId'] = this.serviceId;
data['CategoryId'] = this.categoryId;
data['Category'] = this.category;
data['Description'] = this.description;
data['ServiceResponsePropertyId'] = this.serviceResponsePropertyId;
data['MappingId'] = this.mappingId;
data['IsVisible'] = this.isVisible;
data['PackageRequestId'] = this.packageRequestId;
data['SortOrder'] = this.sortOrder;
if (this.value != null) {
data['Value'] = this.value!.toJson();
}
return data;
}
}
class Value {
String? make;
String? type;
String? model;
int? year;
String? body;
String? driveType;
String? fueType;
Value(
{this.make,
this.type,
this.model,
this.year,
this.body,
this.driveType,
this.fueType});
Value.fromJson(Map<String, dynamic> json) {
make = json['make'];
type = json['type'];
model = json['model'];
year = json['year'];
body = json['body'];
driveType = json['drive_type'];
fueType = json['fue_type'];
}
Map<String, dynamic> toJson() {
final Map<String, dynamic> data = new Map<String, dynamic>();
data['make'] = this.make;
data['type'] = this.type;
data['model'] = this.model;
data['year'] = this.year;
data['body'] = this.body;
data['drive_type'] = this.driveType;
data['fue_type'] = this.fueType;
return data;
}
}

我的反应。控制台中数据响应:

[{Id: 0, SourceId: 0, ServiceId: 11, CategoryId: 5, Category: Valuation, Description: AdjustedValues, Value: [], 
ServiceResponsePropertyId: 474, MappingId: 0, IsVisible: true, PackageRequestId: 13853137, SortOrder: 1}, {Id: 0, SourceId: 0, ServiceId: 11, CategoryId: 1, Category: General, Description: ServiceStatus, Value: {StatusCode: 1, StatusDescription: Ok, StatusDetail: 
, RestServiceStatus: null, ServiceResource: null}, ServiceResponsePropertyId: 475, MappingId: 0, IsVisible: false, PackageRequestId: 13853137, SortOrder: 1}, {Id: 0, SourceId: 0, ServiceId: 6, CategoryId: 1, Category: General, Description: CarId, Value: 120354, ServiceResponsePropertyId: 100, MappingId: 0, IsVisible: false, PackageRequestId: 13853137, SortOrder: 1}, {Id: 0, SourceId: 0, ServiceId: 6, CategoryId: 1, Category: General, Description: Year, Value: 2017, ServiceResponsePropertyId: 103, MappingId: 0, IsVisible: true, PackageRequestId: 13853137, SortOrder: 6}, {Id: 0, SourceId: 0, ServiceId: 6, CategoryId: 1, Category: General, D

我不明白它为什么要打印这个,因为在postman中,它返回的是一个有效的json。

错误出现在这行

return Autogenerated.fromJson(jsonDecode(response.data.toString()));

而不是:

return Autogenerated.fromJson(jsonDecode(response.data.toString()));

试题:

return List<Autogenerated>.from(response.data.map((i) => Autogenerated.fromJson(i)));

如果这不起作用,您应该检查json serializable包,以便自动创建fromJson和toJson方法的过程

嗨,伙计们,我设法找到了解决我的问题的方法,首先我得到了我在问题中发布的错误,因为我的JSON响应无效,所以我必须先编码我的响应,所以我做到了:print (json.encode (response.data));获取一个有效的json响应。

第二个错误是类型'List'不是类型'Map<String,>'的子类型,我使用了这个链接Flutter 'List'不是类型'Map<String,>'的子类型对于帮助,感谢所有在评论中帮助的人。

相关内容

最新更新