Dart分析出现空错误,即使我进行了空检查。

huangapple go评论76阅读模式
英文:

Dart analysis gives null error even if I had performed a null check

问题

错误信息显示:参数类型 'String?' 无法赋值给参数类型 'String'。(在 [sitewalk] lib/modules/floor_plan/widgets/dialog_nested/dialog_nested.dart:122 处的 argument_type_not_assignable)

希望这对您有所帮助。

英文:

I have performed a null check but still I'm getting the below error.

error: The argument type 'String?' can't be assigned to the parameter type 'String'. (argument_type_not_assignable at [sitewalk] lib/modules/floor_plan/widgets/dialog_nested/dialog_nested.dart:122)

Screenshot below for better understanding:
Dart分析出现空错误,即使我进行了空检查。

Now if I use the null assertion operator(!) the error will be gone. But I wanted to know why it's giving an error on the first place?

Is it because if we use multi thread this value can be changed which might cause an error?

答案1

得分: 2

原因实际上是,您的Test类的子类可能会使用一个getter来重写该字段,该getter不能始终返回相同的值。就像这样:

class Test2 extends Test {
  const Test2({super.key, super.errormessage});

  @override
  String? get errorMessage => Random().nextBool() ? null : 'aaa';
}

在这种情况下,空值检查可能第一次通过,但在获取Text时可能返回空值。

英文:

The reason is actually that subclasses of your Test class may override the field with a getter that doesn't consistently give the same value back. Like this:

class Test2 extends Test {
  const Test2({super.key, super.errormessage});

  @override
  String? get errorMessage => Random().nextBool() ? null : 'aaa';
}

In this case the null check might pass the first time but when getting it for the Text it might return null

答案2

得分: 0

你需要一个变量,被视为从 String?String类型提升候选。最简单的方法是创建一个本地变量:

@override
Widget build(BuildContext context) {
  final localErrorMessage = errorMessage;
  if (localErrorMessage == null) {
    return SizedBox.shrink();
  } else {
    return Text(localErrorMessage);
  }
}
英文:

You need a variable, that is considered to be a candidate for type promotion from String? to String. The easiest way is to have a local variable:

@override
Widget build(BuildContext context) {
  final localErrorMessage = errorMessage;
  if(localErrorMessage == null) {
    return SizedBox.shrink();
  } else {
    return Text(localErrorMessage);
  }
}

答案3

得分: 0

class MyClass extends StatelessWidget {
final String? errorMessage;
const MyClass({Key? key, this.errorMessage,}) : super(key: key);
@override
Widget build(BuildContext context) {
if (errorMessage == null) {
return const SizedBox.shrink();
} else {
return Text("$errorMessage");
}
}
}

祝你有个愉快的时间!

英文:

use this code

  class MyClass extends StatelessWidget {
  final String? errorMessage;
  const MyClass({Key? key, this.errorMessage,}) : super(key: key);
    @override
     Widget build(BuildContext context) {
      if (errorMessage == null) {
      return const SizedBox.shrink();
    } else {
      return Text("$errorMessage");
    }
  }
}

have good time;

答案4

得分: -2

把感叹号放在 'errorMessage' 的末尾,就像这样:

class Test extends StatelessWidget {
  final String? errorMessage;

  const Test({
    Key? key,
    this.errorMessage,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (errorMessage == null) {
      return const SizedBox.shrink();
    } else {
      return Text(errorMessage!);
    }
  }
}
英文:

You can do it like:

Put bang operator to end of the 'errorMessage'

class Test extends StatelessWidget {
  final String? errorMessage;

  const Test({
    Key? key,
    this.errorMessage,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    if (errorMessage == null) {
      return const SizedBox.shrink();
    } else {
      return Text(errorMessage!);
    }
  }
}

huangapple
  • 本文由 发表于 2023年5月15日 15:39:07
  • 转载请务必保留本文链接:https://go.coder-hub.com/76251841.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定