我不能运行我的系统,因为有一个错误的用户。ListTile Text(plant.name)中也有错误。参数类型“String?“”不能分配给参数类型“String”。有人能帮帮我吗
The argument type 'List<Plants>?' can't be assigned to the parameter type 'List<Plants>'.dartargument_type_not_assignable List<Plants>? users
body: FutureBuilder<List<Plants>>(
future: PlantsApi.getPlantsLocally(context),
builder:(context, snapshot) {
final users = snapshot.data;
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
if (snapshot.hasError) {
return Center(child: Text('Some error'),);
}
else {
return buildPlants(users);
}
}
},
Widget buildPlants(List<Plants> plants) =>
ListView.builder(
itemCount: plants.length,
itemBuilder: (context, index) {
final plant = plants[index];
return
ListTile (
title: Text(plant.name),
);
}
);
我的获取API
class PlantsApi {
static Future<List<Plants>> getPlantsLocally(BuildContext context) async {
final assetBundle = DefaultAssetBundle.of(context);
final data = await assetBundle.loadString('assets/plants.json');
final body = json.decode(data);
return body.map<Plants>((e) => Plants.fromJson(e)).toList();
}
}
这是json using data类的示例
[
{
"id": 1,
"name": "ALOCASIA-ELEPHANT EARS",
"image": "assets/images/ALOCASIA.jpeg",
"descript": "Alocasia plant (Alocasia mortfontanensis) is a hybrid species between Alocasia longiloba and Alocasia sanderiana. The Alocasia is known for its large leaves and wide variety of cultivars within the species. Alocasia plant is native to tropical Asia and Australia.",
"charac": [{
"planttype": "Herb",
"lifespan": "Perennial",
"bloomtime": "Spring, summer",
"plantheight": "1-2 feet",
"spread": "7 feet",
"leafColor": "PurpleGreenGreySilver"
}
],
"scienclass": [
{
"genus": "Alocasia - Elephant's-ears, Taro, Kris plant",
"family": " Araceae - Arum, Aroids ",
"order": "Alismatales - Water plantains and allies",
"classes": "Liliopsida - Monocotyledons, Monocots ",
"phylum":"Tracheophyta - Vascular plants, Seed plants, Ferns, Tracheophytes"
}
],
"pestdesease": " Stem rot, crown rot, root rot, leaf spot, mealy bugs, aphids",
"requirements":[{
"difficultyrating": "Alocasia plant is super easy to take care of, with resistance to almost all pests and diseases. It is a perfect option for gardeners with brown thumbs.",
"sunlight": "Full shade to partial sun",
"hardenesszone": " 9-11 ",
"soil": "Loose, fertile and well-drained humus soil"
}
],
"careguide":[
{
"water": "Moisture-loving, keep the soil moist but do not let water accumulate.",
"fertilizaton": "Fertilization once in spring. ",
"pruning": "Fertilization once in spring. ",
"plantingtime": "Spring, summer, autumn ",
"propagation": "Division ",
"pottingsuggestion": " Needs excellent drainage in pots."
}
],
"toxictohuman": "Is the alocasia plant poisonous for humans? The sap of alocasia plant is toxic to humans topically and when ingested. When the leaves are chewed or swallowed, symptoms may include swelling, stinging, and irritation of the mouth and gastrointestinal tract. In rare cases, it can cause swelling of the upper airway and difficulty breathing. Contact with the sap can also lead to skin irritation where the contact occurred. Poisoning is most likely to occur from accidental ingestion of the leaves or rough handling of the plant, particularly by children. Alocasia plant is often encountered as an ornamental plant in gardens or as a houseplant."
},
{
"id": 2,
"name": "Sample Sample",
"image": "assets/images/ALOCASIA.jpeg",
"descript": "Alocasia plant (Alocasia mortfontanensis) is a hybrid species between Alocasia longiloba and Alocasia sanderiana. The Alocasia is known for its large leaves and wide variety of cultivars within the species. Alocasia plant is native to tropical Asia and Australia.",
"charac": [{
"planttype": "Herb",
"lifespan": "Perennial",
"bloomtime": "Spring, summer",
"plantheight": "1-2 feet",
"spread": "7 feet",
"leafColor": "PurpleGreenGreySilver"
}
],
"scienclass": [
{
"genus": "Alocasia - Elephant's-ears, Taro, Kris plant",
"family": " Araceae - Arum, Aroids ",
"order": "Alismatales - Water plantains and allies",
"classes": "Liliopsida - Monocotyledons, Monocots ",
"phylum":"Tracheophyta - Vascular plants, Seed plants, Ferns, Tracheophytes"
}
],
"pestdesease": " Stem rot, crown rot, root rot, leaf spot, mealy bugs, aphids",
"requirements":[{
"difficultyrating": "Alocasia plant is super easy to take care of, with resistance to almost all pests and diseases. It is a perfect option for gardeners with brown thumbs.",
"sunlight": "Full shade to partial sun",
"hardenesszone": " 9-11 ",
"soil": "Loose, fertile and well-drained humus soil"
}
],
"careguide":[
{
"water": "Moisture-loving, keep the soil moist but do not let water accumulate.",
"fertilizaton": "Fertilization once in spring. ",
"pruning": "Fertilization once in spring. ",
"plantingtime": "Spring, summer, autumn ",
"propagation": "Division ",
"pottingsuggestion": " Needs excellent drainage in pots."
}
],
"toxictohuman": "Is the alocasia plant poisonous for humans? The sap of alocasia plant is toxic to humans topically and when ingested. When the leaves are chewed or swallowed, symptoms may include swelling, stinging, and irritation of the mouth and gastrointestinal tract. In rare cases, it can cause swelling of the upper airway and difficulty breathing. Contact with the sap can also lead to skin irritation where the contact occurred. Poisoning is most likely to occur from accidental ingestion of the leaves or rough handling of the plant, particularly by children. Alocasia plant is often encountered as an ornamental plant in gardens or as a houseplant."
}
]
我使用JSON的数据类将其转换为dart
2条答案
按热度按时间qzwqbdag1#
未来的构建器将始终具有可空数据类型,即
snapshot.data
的类型为[your_type]?
,因此,代码必须编写如下:关于
Text(plant.name)
的问题,这是因为Text
小部件需要一个不可空的String,但Plant类的name
是一个可空的String。所以要解决这个问题,你可以给予它一个默认值,如果它是空的,即
plant.name ?? 'default'
或者在类中将类型更改为不可空。k2arahey2#
The argument type 'List<Plants>?' can't be assigned to the parameter type 'List<Plants>'
请注意
?
。这表明您试图传递的值可能会取空值,但接收方只接受植物列表(List<Plants>
没有最终的?
)。你可以重构你的代码,这样你传递给你的函数的变量就永远不会为null,并将它示例化为
List<Plants> yourVariable = aListOfPlants
。一个想法是将默认值设置为空列表= List<Plant>[]
。如果上面的解决方案不可行,你可以Assert你的参数不为null,用一个bang
!
,如下所示:yourFunctionWhichDoesNotAcceptNullValues(variableWhichIsInstantiatedAsNullable!)
。这将允许您编译并继续,但您应该手动防止
null
被传递到yourFunctionWhichDoesNotAcceptNullValues
。