Reputation: 765
Hey I have trouble with the Null check operator used on a null value error. In general I nearly have no problems with this, just in one case.
So I have a site where you can chose between two options & this works always, but in this one case not
Because of that I thought that I have flaws in my Dataset, so that a value just isnt defined. I checked & it seemed correct:
Here I coppied in a part of the flawed data:
{
"title":"105 PS",
"unterscheidungsData":[
{
"difference":"Hubraum 1422 cm³",
"title":"1.4 TDI DPF BlueMotion Technology",
"wholeText ":"Ölverlust\n\nBremse\n\nBatterie(2009-2013)\n\nSteuerkette(2009 - 2011)\n\nZündspule(2009 - 2010)\n\nZündkerze(2009 - 2013)\n\nScheinwerfer(2010)"
},
{
"difference ":"Hubraum 1598 cm³",
"title ":"1.6 TDI(DPF)",
"wholeText ":"Ölverlust\n\nBremse\n\nBatterie(2009-2013)\n\nSteuerkette(2009 - 2011)\n\nZündspule(2009 - 2010)\n\nZündkerze(2009 - 2013)\n\nScheinwerfer(2010)"
}
]
}
& here is data that works perfectly fine, I compared them & didnt really notice any differences in the syntax
{
"title":"150 PS",
"unterscheidungsData":[
{
"difference":"Sportwagen, Crosswagon",
"title":"1.9 16V JTD M-Jet",
"wholeText":"abbrechende Drallklappen können zu Turbo- oder Motorschaden führen\n\nDefekte Ölpumpen\n\nDefekte Lichtmaschinen\n\nverschlissene Zweimassenschwungräder\n\nVerrußte und hängende Abgasrückführungsventile\n\nAusfall Bremsservo\n\nUndichte Wasserkühler auf der Innenseite\n\ngerissene Keilrippenriemen\n\ndefekte Anlasser"
},
{
"difference":"Normal",
"title":"2.4 10V JTD",
"wholeText":"Verrußte und hängende Abgasrückführungsventile\n\nAusfall Bremsservo\n\nUndichte Wasserkühler auf der Innenseite\n\ngerissene Keilrippenriemen\n\ndefekte Anlasser\n\nBeschädigungen der Ölwanne"
}
]
},
So lastly I thought that my code has some flaws (this is my listview builder):
ListView.builder(
itemCount: this.leistungData!.unterscheidungsData!.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {
Navigator.push(context,
MaterialPageRoute(builder: (context) => AutoProbleme(
variantData: variantData,
unterscheidungsData: leistungData!.unterscheidungsData![index],
modell: modell,
modelData: modelData,
)));
},
child: Container(
margin: EdgeInsets.all(10),
height: MediaQuery.of(context).size.height * 0.05,
width: MediaQuery.of(context).size.width * 0.9,
child: Align(
alignment: Alignment.center,
child: Text(
this.leistungData!.unterscheidungsData![index].difference!,
textScaleFactor: 1.4,
),
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16)),
),
)),
& I have to say that I didnt find anything that would justify this behaviour. For me the data looks fine & it works nearly always, but nevertheless I get this error. Do you have any ideas what the problem could be?
Upvotes: 0
Views: 184
Reputation: 5638
You should make sure that all expressions with a nullable type aren't null if you are using the null assertion operator !
.
If you’re sure that an expression with a nullable type isn’t null, you can use a null assertion operator (!) to make Dart treat it as non-nullable. By adding ! just after the expression, you tell Dart that the value won’t be null, and that it’s safe to assign it to a non-nullable variable.
Pay attention to the following lines from the docs:
If you’re wrong, Dart throws an exception at run-time. This makes the ! operator unsafe, so don’t use it unless you’re very sure that the expression isn’t null.
bad ❌
int? number;
print(number! > 0);
good ✅
int? number;
if(number != null) {
print(number > 0);
}
I would recommend that you check whether leistungData
is null and show a loading state using CircularProgressIndicator
in case it is null:
@override
Widget build(BuildContext context) {
if (leistungData == null || leistungData?.unterscheidungsData == null) {
return CircularProgressIndicator();
}
return ListView.builder(
itemCount: leistungData!.unterscheidungsData!.length,
itemBuilder: (context, index) => GestureDetector(
onTap: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => AutoProbleme(
variantData: variantData,
unterscheidungsData: leistungData!.unterscheidungsData![index],
modell: modell,
modelData: modelData,
),
),
);
},
child: Container(
margin: EdgeInsets.all(10),
height: MediaQuery.of(context).size.height * 0.05,
width: MediaQuery.of(context).size.width * 0.9,
child: Align(
alignment: Alignment.center,
child: Text(
leistungData!.unterscheidungsData![index]?.difference ?? '',
textScaleFactor: 1.4,
),
),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(16),
),
),
),
);
}
Upvotes: 3
Reputation: 34250
This error comes when non-null operator is used on a null value
int? data;
void main() {
print(data!.value);
}
The above code will give an error if the data value is not assigned.
Solution:
?.
operator used to check null values, and ??
is used to provide default values if the given value is null
int? data;
void main() {
print(data?.value ?? "Default Value");
}
Upvotes: 2