英文:
Dart optional vlaue as prop
问题
我是Flutter/Dart的新手,但我有Typescript的经验。你能帮我处理以下的代码和问题吗?
```dart
final String? title;
appBar: title != null
? AppBar(
title: Text(title),
)
: null,
我收到了错误消息“参数类型'字符串?'无法分配给参数类型'字符串'”。
我以为条件title != null
会确保title
不为空。有没有更好的处理方法?而不仅仅是添加Text(title!),
?
<details>
<summary>英文:</summary>
I am new to Flutter/Dart but I have experience with Typescript. Can you help me with the following code and question:
```dart
final String? title;
appBar: title != null
? AppBar(
title: Text(title),
)
: null,
I am getting the error message The argument type 'String?' can't be assigned to the parameter type 'String'
.
I thought that the condition title != null
would ensure that title
is not null. Is there a better way to handle this? to not add just Text(title!),
?
答案1
得分: 1
只有局部变量可以这样使用,因为存在title
第二次访问时可能返回null
的情况。所以,如果你真的不想使用!
,你可以这样做:
@override
Widget build(BuildContext context) {
final t = title;
return Scaffold(
appBar: t != null
? AppBar(
title: Text(t),
)
: null,
为了举例说明为什么不能保证title
第二次不为空,请考虑以下程序:
import 'dart:math';
main() {
var b = B('whatever');
b.printTitleLength();
}
class A {
final String? title;
A(this.title);
void printTitleLength() {
if (title != null) {
print(title!.length);
}
}
}
class B extends A {
B(super.title);
@override
String? get title => Random().nextBool() ? 'test' : null;
}
请注意,我不得不在print行中放置!
,否则它不会编译。但还要注意,由于在空检查之后title
有可能返回null,因此这可能会抛出异常。
英文:
Only local variables can be used like that because there are situations where the title
can return null
the second time it's accessed. So you could do this if you really don't want to use !
@override
Widget build(BuildContext context) {
final t = title;
return Scaffold(
appBar: t != null
? AppBar(
title: Text(t),
)
: null,
To give an example of why it's not guaranteed that title
is not null the second time consider this program:
import 'dart:math';
main() {
var b = B('whatever');
b.printTitleLength();
}
class A {
final String? title;
A(this.title);
void printTitleLength() {
if (title != null) {
print(title!.length);
}
}
}
class B extends A {
B(super.title);
@override
String? get title => Random().nextBool() ? 'test' : null;
}
Note I had to put the !
in the print line otherwise it wouldn't compile. But also note that this possibly can throw an exception since it's possible title
returns null after the null check.
答案2
得分: 1
使用空安全操作符在标题为null时分配默认值,因为Text小部件期望一个非null值。
final String? title;
appBar: title != null
? AppBar(
title: Text(title ?? ""),
)
: null,
英文:
use null aware operator to assign a default value when title is null, as Text widget expects a non null value.
final String? title;
appBar: title != null
? AppBar(
title: Text(title??""),
)
: null,
答案3
得分: 1
你现在可以使用 switch 表达式来解决这个问题:
appbar: switch(title) { String t => AppBar(title: t), _ => null },
这能够正常工作,因为title被复制到一个新的t变量中,并且保证不为空。如果它为空,就会进入通用情况。
英文:
You can use a switch expression to solve this now:
appbar: switch(title) { String t => AppBar(title: t), _ => null },
This works because title is copied into a new t variable, and guaranteed to be non-null. If it's null, it falls through to the catch-all.
答案4
得分: 0
Dart是空安全的,
所以你不能在不给它一个初始值的情况下定义变量,
你可以定义一个初始值,它将是一个空字符串,或者如果你确信在将其分配到屏幕之前会给该变量赋值,你可以使用late关键字,注意,如果你误用它并在构建屏幕之前未赋值,将在屏幕上获取空错误。
如果你希望它接受title可以是一个字符串或空值,你可以在String后面加上**?**。
如果你想使用该变量,需要使用**null断言操作符(!)**告诉Dart在运行时确保它不会为空。
请参考此文档,以更好地了解Dart中的空安全性:空安全性
英文:
Dart is null-safe,
so you can't define a variable without giving it an init value,
you may define an init value and it will be an empty string or if you are sure that you will give value to that variable before assigning it to the screen you can use late keyword, Note that if you misuse it and didn't assign a value before the screen is built, you will get a null error in the screen.
if you want it to accept that title could be a string or null you put the ? after String.
if you want to use that variable you need to put The null assertion operator (!)
telling dart that you are sure it won't be null during runtime
refer to this documentation to have a better understanding of Null Safety in Dart Null Safety
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论