我有一个没有数据的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;
}
这是一个具有映射函数的类:
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(只是为了从我正在使用的AS400数据库中获得良好的大小)
问题列表在getOrderReceipt(orderno)
的未来,未来打印decoded
,但没有返回任何从await decoded.map<OrderReceipt>((json) => OrderReceipt.fromJson(json)).toList();
。
我没有得到任何错误-构建Future<List<OrderReceipt>>
的FutureBuilder正在处理像两秒钟,然后没有数据。我有另外两个FutureBuilders在我的项目和没有任何问题。有人知道这个错误吗?
谢谢. .
编辑1:
@Jozott, @Eric Martin和@pmatatias:我已经更新了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(
...
...
); }
...
...
编辑2:这是在注释中讨论的FutureFunction与try-catch-Block:
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
我还试着注释掉"LUKETXT"在模型中-然后它就工作了。也许这个错误可以抛出,因为值中可以有点,冒号和其他特殊字符?→但是正如我在devtools中看到的,这些特殊字符在json.decode函数中被正确解码为每个头,并且仍然是字符串。
您在名称字段中打错了字。LUKETX而不是LUKETXT
在你的工厂:
LUKETXT: json['LUKETX'] as String ?? '',
您的代码试图将空值强制转换为字符串。这就是为什么你会得到这个错误。
正如@jozott所提到的,你可以使用像json_serializable这样的代码生成器代码来避免拼写错误:https://pub.dev/packages/json_serializable
顺便说一下,我认为您不必将强制转换声明为string。因为dart可以推断出类型。想象一个字符串字段名,你可以这样写:
User.fromJson(Map<String, dynamic> json)
: name = json['name'] ?? ''