[英]Error: Null check operator used on a null value - for boolean value - in Flutter
https://flutterigniter.com/checking-null-aware-operators-dart/ https://flutterigniter.com/checking-null-aware-operators-dart/
Null check operator used on a null value 用于空值的空检查运算符
These links say that if the value can be null then we can use ??
这些链接说如果值可以为空,那么我们可以使用
??
operator to assign a value to it.运算符为其赋值。 I tried it, it still shows the same error:
我试过了,它仍然显示相同的错误:
This is the data structure .这就是数据结构。
main.dart主要.dart
import 'package:flutter/material.dart';
import 'dart:convert';
BigData bigDataFromJson(String str) => BigData.fromJson(json.decode(str));
class BigData {
BigData({
this.data,
});
Data? data;
factory BigData.fromJson(Map<String, dynamic> json) => BigData(
data: Data.fromJson(json["data"]),
);
Map<String, dynamic> toJson() => {
"data": data!.toJson(),
};
}
class Data {
Data({
this.lockoutDetails,
});
bool? lockoutDetails;
factory Data.fromJson(Map<String, dynamic> json) => Data(
lockoutDetails: json["lockout_details"],
);
Map<String, dynamic> toJson() => {
"lockout_details": lockoutDetails,
};
}
Here from the default application of Flutter starts:这里从 Flutter 的默认应用开始:
void main() {
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
@override
State<MyHomePage> createState() => _MyHomePageState();
}
Here I have used the variable lockoutDetails
from the above datastructures and it shows the error null check operator used on a null value
.在这里,我使用了上述数据结构中的变量
lockoutDetails
,它显示了null check operator used on a null value
。
class _MyHomePageState extends State<MyHomePage> {
BigData d = BigData();
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child:
d.data!.lockoutDetails ?? true
? const Text(
'AAA',
)
: Container(),
));
}
}
What is the way to correct it?改正的方法是什么?
d.data
is null in this case, you can replace d.data
在这种情况下为空,您可以替换
d.data!.lockoutDetails ?? true
with和
d.data?.lockoutDetails ?? true
Your d.data
is null.您的
d.data
为空。 For test run use d.data?.lockoutDetails
.对于测试运行,请使用
d.data?.lockoutDetails
。
Think to set data for to have value.认为设置数据是有价值的。
you can use ?您可以使用 ? instead of !
代替 ! which means that it may or maynot be null.
这意味着它可能为空,也可能不为空。 If you add !
如果添加! you are mentioning that its not null but the value is unknown
你提到它不是空的,但值是未知的
d.data?.lockoutDetails ?? true
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.