Reputation: 21
I cant run my system because of having an error on users. and also in ListTile Text(plant.name) is error. The argument type 'String?' can't be assigned to the parameter type 'String'. Can someone help me?
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),
);
}
);
my fetch 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();
}
}
this is the sample of the json using data class
[
{
"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."
}
]
i use data class for the json to convert it to dart
Upvotes: 0
Views: 593
Reputation: 144
The argument type 'List<Plants>?' can't be assigned to the parameter type 'List<Plants>'
Notice the ?
. That indicates that the value you are trying to pass can possibly take a null value, but the receiver only accepts lists of plants (List<Plants>
has no final ?
).
You could refactor your code so that the variable you pass to your function is never null, and instantiate it as such List<Plants> yourVariable = aListOfPlants
. One idea would be to make the default value be an empty list, = List<Plant>[]
.
If the above solution isn't feasible, you can assert that your argument is not null, with a bang !
, as in:
yourFunctionWhichDoesNotAcceptNullValues(variableWhichIsInstantiatedAsNullable!)
.
This will allow you to compile and proceed, but you should manually guard against null
ever being passed into yourFunctionWhichDoesNotAcceptNullValues
.
Upvotes: 0
Reputation: 619
Future builders will always have a nullable data type, i.e. snapshot.data
is of type [your_type]?
Because of that, the code has to be written as below :
FutureBuilder<List<Plants>>(
future: PlantsApi.getPlantsLocally(context),
builder:(context, snapshot) {
switch (snapshot.connectionState) {
case ConnectionState.waiting:
return Center(child: CircularProgressIndicator());
default:
if (snapshot.hasData) {
return buildPlants(snapshot.data!)
} else {
return Center(child: Text('Some error'),);
}
}
}
Regarding your issue with Text(plant.name)
, this is because the Text
widget required a non-nullable String, but your Plant class's name
is a nullable String.
So to resolve the issue you can either give it a default value if its null, i.e. plant.name ?? 'default'
or you change the type to non-nullable in your class.
Upvotes: 1