我想检查列表是否为空,然后显示一些图像,如果不是,我想显示列表的内容,但在检查列表是否为空时出现错误。这是我的模型类>
Padding(
padding: const EdgeInsets.all(8.0),
child: Container(
decoration: BoxDecoration(
border: Border.all(
color: Colors.orange,
),
borderRadius: BorderRadius.circular(20)),
height: MediaQuery.of(context).size.height / 2.5,
width: MediaQuery.of(context).size.width - 100,
child: addProductCtrl.product.sizes!.length > 0
? Padding(
padding: const EdgeInsets.all(8.0),
child: GetBuilder<AddProductCtrl>(
id: editDetails,
builder: (_) {
return new ListView.builder(
scrollDirection: Axis.vertical,
itemCount: addProductCtrl.product.sizes!.length,
itemBuilder: (_, index) => Row(
mainAxisAlignment:
MainAxisAlignment.spaceBetween,
children: [
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text(
addProductCtrl.product.sizes![index],
style: TextStyle(
fontSize: 25)),
),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text(
"Rs.${addProductCtrl.product.price![index]}",
style: TextStyle(
fontSize: 25)),
),
Padding(
padding:
const EdgeInsets.all(8.0),
child: Text(
addProductCtrl.product.quantity![index],
style: TextStyle(
fontSize: 25)),
),
],
));
}),
)
: Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: Text("Add Details ",
style: TextStyle(fontSize: 25))),
),
),
),
但是我在空值上使用了空检查运算符。有人可以指出这里出了什么问题。谢谢
这是我的代码用法:
{{1}}
错误图片
答案 0 :(得分:0)
基于 JSON 示例,我将您的 JSON 解析器重写为以下内容,这也是 nullsafe:
class ProductModel {
ProductModel({
required this.name,
required this.description,
required this.sellerId,
required this.published,
required this.origin,
required this.weight,
required this.material,
required this.photoUrl,
required this.discount,
required this.price,
required this.quantity,
required this.sizes,
});
String name;
String description;
String sellerId;
String published;
String origin;
String? weight;
String? material;
List<String> photoUrl;
int? discount;
List<String> sizes;
List<String> price;
List<String> quantity;
factory ProductModel.fromRawJson(String str) =>
ProductModel.fromJson(json.decode(str) as Map<String, dynamic>);
String toRawJson() => json.encode(toJson());
factory ProductModel.fromJson(Map<String, dynamic> json) => ProductModel(
name: json["name"] as String,
description: json["description"] as String,
sellerId: json["sellerId"] as String,
published: json["published"] as String,
origin: json["origin"] as String,
weight: json["weight"] as String?,
material: json["material"] as String?,
photoUrl: (json["photoUrl"] as List?)?.cast<String>().toList() ?? [],
discount: json["discount"] as int?,
sizes: (json["sizes"] as List?)?.cast<String>().toList() ?? [],
price: (json["price"] as List?)?.cast<String>().toList() ?? [],
quantity: (json["qty"] as List?)?.cast<String>().toList() ?? [],
);
Map<String, dynamic> toJson() => <String, dynamic>{
"name": name,
"description": description,
"sellerId": sellerId,
"published": published,
"origin": origin,
if (weight != null) "weight": weight,
if (material != null) "material": material,
if (photoUrl.isNotEmpty) "photoUrl": photoUrl,
if (discount != null) "discount": discount,
if (sizes.isNotEmpty) "sizes": sizes,
if (price.isNotEmpty) "price": price,
if (quantity.isNotEmpty) "qty": quantity,
};
}
const jsonInput = '''{
"name": "test",
"description": "",
"sellerId": "",
"published": "",
"origin": "",
"weight": "",
"material": "",
"photoUrl": [],
"discount": 10,
"size": [],
"price": [],
"qty": []
}
''';
void main() {
print(ProductModel.fromRawJson(jsonInput).toJson());
// {name: test, description: , sellerId: , published: , origin: , weight: , material: , discount: 10}
}
我不确定您希望 toJson
方法如何工作,但我添加了一些关于如何制作输出的示例,以便它不包含字段,例如空列表。