简体   繁体   English

dart有什么区别? 和? 对于可空类型?

[英]In dart what is the difference between ? and ! for nullable types?

I am new to Dart and Flutter.我是 Dart 和 Flutter 的新手。

In dart what is the difference between using?在dart中使用有什么区别? and?和? for null-able types?对于可为空的类型?

validator: ((value) {
   if (value?.isEmpty) {
        return "Field is required";
   }
        return null;
   }),


validator: ((value) {
   if (value!.isEmpty) {
        return "Field is required";
   }
        return null;
   }),

Thanks in advance!提前致谢!

Good topic about it: What is Null Safety in Dart?关于它的好话题: Dart中的Null Safety是什么?

But in short, you use "?"但简而言之,您使用“?” when you want to allow the value to be null and use it accordingly, like this:当您想允许该值为 null 并相应地使用它时,如下所示:

String? test;
if (test?.isEmpty == true) { // And is not null, but you don't need to check it
  // If null, will never pass there but without error
}

And use "," when you want to be sure to have a non nullable value: like this:当你想确保有一个不可空的值时使用“,”:像这样:

String? test;
if (test!.isEmpty == true) { // Will throw an error
  ...
}

the difference between the two,one can be null initially, but the other cannot.两者的区别,一个初始可以是null,另一个不能。

I hope you understand in the example below.我希望你能理解下面的例子。

To specify if the variable can be null, then you can use the nullable type?指定变量可以是null,那么可以使用nullable类型?
operator, Lets see an example:运算符,让我们看一个例子:

String? carName;  // initialized to null by default
int? value = 36;  // initialized to non-null
value = null; // can be re-assigned to null

Note: You don't need to initialize a nullable variable before using it.注意:您不需要在使用前初始化可空变量。 It is initialized to null by default.默认初始化为null。

The Assertion Operator (!)断言运算符 (!)

Use the null assertion operator (. ) to make Dart treat a nullable expression as non-nullable if you're certain it isn't null.使用 null 断言运算符 (.) 使 Dart 将可空表达式视为不可空表达式(如果您确定它不为空)。

int? someValue = 30;
int data = someValue!; // This is valid as value is non-nullable

In the above example, we are telling Dart that the variable someValue is null, and it is safe to assign it to a non-nullable variable ie data在上面的例子中,我们告诉 Dart 变量 someValue 是 null,将它赋值给一个不可为 null 的变量即 data 是安全的

I hope you understand????我希望你明白????

As for your example;至于你的例子;

if you notice, the validator {String?如果你注意到,验证器 {String? value} value can initially be null. but the only difference between both works in the code you wrote will be the running cost. value} 值最初可以是 null。但是您编写的代码中两者之间的唯一区别是运行成本。 '?' “?” it will cost some time when you define it again.重新定义它会花费一些时间。 because it is already stated in the function that it will be null as a start.因为在 function 中已经说明了,它将以 null 作为开始。

It's a good question and the answer is here as a person.这是一个很好的问题,答案就在这里。 '?' “?” it means it will get value later or it can be null( initially or at any instance) for example这意味着它将在以后获得价值,或者它可以为空(最初或在任何情况下)例如

String?细绳? carName;车名;

  1. '.' '.' it means you are going to receive the value and it can not be null. it will check the value if the value is null it will give exception.这意味着您将收到该值,它不能是 null。如果值为 null,它将检查该值,它将给出异常。

have a look on example for clear difference:查看示例以了解明显的区别:

List?列表? blocks;块; ... // you are not sure blocks variable is initialized or not. ... // 您不确定 blocks 变量是否已初始化。 // block is nullable. // 块可以为空。 final Block?最终块? block = blocks?.first; block = blocks?.first;

// you are sure blocks variable is initialized. // 你确定 blocks 变量已经初始化。 // block is not nullable. // 块不可为空。 final Block block = blocks.;first; final Block block = blocks.;first;

hope you got it if yes accept the answer or comment me if you have question如果是,希望你明白了 如果你有问题,请接受答案或评论我

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM