错误:空检查操作员在零值上使用 - 用于布尔值 -

发布于 2025-02-12 19:10:14 字数 2125 浏览 0 评论 0原文

https://flutterigniter.com/checking-null-aware-aware-aware-aware-operators-dart/ /a>

null check ock ock ock operator in null value

值可以为null,然后我们可以使用??操作员为其分配一个值。我尝试过,它仍然显示出相同的错误:

这是数据结构

main.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,
      };
}

此处从拖放的默认应用程序开始:

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();
}

在这里,我从上面的数据架构中使用了变量lockoutdetails,它显示了使用的错误null检查操作员零值

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(),
    ));
  }
}

纠正它的方法是什么?

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


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:

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.

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

司马昭之心 2025-02-19 19:10:14

你可以使用吗?而不是!这意味着它可能或可能无效。如果添加!您提到它不是零,但值是未知的

d.data?.lockoutDetails ?? true

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
一个人的旅程 2025-02-19 19:10:14

d.data在这种情况下是无效的,您可以替换

 d.data!.lockoutDetails ?? true

 d.data?.lockoutDetails ?? true

d.data is null in this case, you can replace

 d.data!.lockoutDetails ?? true

with

 d.data?.lockoutDetails ?? true
月亮坠入山谷 2025-02-19 19:10:14

您的d.data为空。
为了进行测试,请使用d.data?.lockoutdetails

考虑设置数据以具有价值。

Your d.data is null.
For test run use d.data?.lockoutDetails.

Think to set data for to have value.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文