我有一个没有数据的FutureBuilder。我有这个函数从后端获取数据:
Future<List<OrderReceipt>> getOrderReceipt(orderno) async {
final response = await HttpService.order_receipt(orderno);
var decoded = json.decode(response.body)["qry"].cast<Map<String,dynamic>>();
print(decoded);
var OrderR = await decoded.map<OrderReceipt>((json) => OrderReceipt.fromJson(json)).toList();
return OrderR;
}
这是具有Map函数的类:
class OrderReceipt {
final String LUKEBLART;
final String LUKEBLOART;
final String LUKEBLONR;
final String LUKEBLVC;
final String LUKEBLBNR;
final String LUKEBLDRAKT;
final String LUKEBLADS;
final String LUKEBLDATUM;
final String LUKEBLPGM;
final String LUKETIME;
final String LUKESART;
final String LUKEPAGE;
final String LUKELINE;
final String LUKELNR;
final String LUKETXT;
OrderReceipt({
required this.LUKEBLART,
required this.LUKEBLOART,
required this.LUKEBLONR,
required this.LUKEBLVC,
required this.LUKEBLBNR,
required this.LUKEBLDRAKT,
required this.LUKEBLADS,
required this.LUKEBLDATUM,
required this.LUKEBLPGM,
required this.LUKETIME,
required this.LUKESART,
required this.LUKEPAGE,
required this.LUKELINE,
required this.LUKELNR,
required this.LUKETXT,
});
factory OrderReceipt.fromJson(Map<String, dynamic> json) {
return OrderReceipt(
LUKEBLART: json['LUKEBLART'] as String ?? '',
LUKEBLOART: json['LUKEBLOART'] as String ?? '',
LUKEBLONR: json['LUKEBLONR'] as String ?? '',
LUKEBLVC: json['LUKEBLVC'] as String ?? '',
LUKEBLBNR: json['LUKEBLBNR'] as String ?? '',
LUKEBLDRAKT: json['LUKEBLDRAKT'] as String ?? '',
LUKEBLADS: json['LUKEBLADS'] as String ?? '',
LUKEBLDATUM: json['LUKEBLDATUM'] as String ?? '',
LUKEBLPGM: json['LUKEBLPGM'] as String ?? '',
LUKETIME: json['LUKETIME'] as String ?? '',
LUKESART: json['LUKESART'] as String ?? '',
LUKEPAGE: json['LUKEPAGE'] as String ?? '',
LUKELINE: json['LUKELINE'] as String ?? '',
LUKELNR: json['LUKELNR'] as String ?? '',
LUKETXT: json['LUKETX'] as String ?? '',
);}
}
下面是json的一个例子(只有一个数据集):
[{LUKEBLADS: 222222, LUKEBLART: W , LUKEBLBNR: 333333, LUKEBLDATUM: 20230113, LUKEBLDRAKT: FX , LUKEBLOART: FX , LUKEBLONR: 4444, LUKEBLPGM: XXXKG , LUKEBLVC: X1 , LUKELINE: 41, LUKELNR: 36, LUKEPAGE: 1, LUKESART: 1, LUKETIME: Fri, 13 Jan 2023 09:54:02 GMT, LUKETXT: ExampleText USD 66,20 },]
LUKETXT中有很多空格,因为我正在用等宽字体生成一个.pdf(只是为了从我正在使用的AS 400数据库中获得良好的大小调整)
问题出在getOrderReceipt(orderno)
Future中,Future打印decoded
,但未从await decoded.map<OrderReceipt>((json) => OrderReceipt.fromJson(json)).toList();
返回任何内容。
我没有收到任何错误-构建Future<List<OrderReceipt>>
的FutureBuilder处理了大约两秒钟,然后就没有数据了。我的项目中还有另外两个FutureBuilder,它们没有任何问题。有人知道错误吗?
谢谢
编辑1:
@约佐特,@埃里克·马丁和@帕玛蒂亚斯:我已经更新了FutureFunction并删除了.cast<Map<String,dynamic>>()
和await
,但执行中没有任何变化-错误仍然存在,函数以不从decoded.map<OrderReceipt>((json) => OrderReceipt.fromJson(json)).toList();
返回任何内容结束-我更新的函数:
Future<List<OrderReceipt>> getOrderReceipt(orderno) async {
final response = await HttpService.order_receipt(orderno);
print(response);
var decoded = json.decode(response.body)["qry"];
var OrderR = decoded.map<OrderReceipt>((json) => OrderReceipt.fromJson(json)).toList();
print(OrderReceipt);
return OrderR;
}
我还尝试将工厂函数更改为:
...
...
factory OrderReceipt.fromJson(json) {
return OrderReceipt(
...
...
); }
...
...
**EDIT 2:**这是在带有try-catch-Block的FutureFunction的注解中讨论的:
Future<List<OrderReceipt>> getOrderReceipt(orderno) async {
try {
final response = await HttpService.order_receipt(orderno);
print(response);
var decoded = json.decode(response.body)["qry"];
var OrderR = decoded.map<OrderReceipt>((json) =>
OrderReceipt.fromJson(json)).toList();
return OrderR;
} catch(err) {
print('Caught error: $err');
throw 'Caught error: $err';
}
}
显示以下错误:
type 'null' is not a subtype of type 'string' in type cast
我还尝试注解掉Modell中的“LUKETXT”--然后它就工作了。也许这个错误会被抛出,因为值中可能有点,冒号和其他特殊字符?-〉但正如我在devtools中看到的,这些特殊字符在json.decode函数中被正确解码到每个头部,并保持为字符串。
1条答案
按热度按时间vq8itlhq1#
您在名称字段中输入了一个拼写错误。LUKETX而不是LUKETXT
在您的工厂:
您的代码试图将空值转换为字符串。这就是为什么会出现此错误。
正如@jozott所提到的,您可以使用像json_serializable这样的代码生成器代码来避免拼写错误:https://pub.dev/packages/json_serializable
顺便说一句,我不认为你必须声明强制转换为字符串,因为dart会推断类型。想象一个字符串字段名,你可以这样写: