Flutter - 参数类型 'String?' 无法分配给参数类型 'String'
Flutter - The argument type 'String?' can't be assigned to the parameter type 'String'
Flutter 2.0引入null safety之后,但是在获取String变量的时候Flutter报错-
The argument type 'String?' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)
我想要一个构造函数,如何实现带空安全的构造函数?还有如何获取字符串变量。
我也用过 toString() 方法,但不确定它是否是真正的方法。
class OnbordingSliderTile extends StatelessWidget {
final String? title;
OnbordingSliderTile({this.title});
@override
Widget build(BuildContext context) {
return Container(
//This Text widget is showing the error
child: Text(title)
);
}
}
Type String?
表示此类型的变量必须有字符串或空值。在您的情况下,您可能总是需要一个标题,并且此变量不应为空。
所以解决方案是将类型更改为 String
并将 required
关键字添加到构造函数中的命名参数。
class OnbordingSliderTile extends StatelessWidget {
final String title;
OnbordingSliderTile({required this.title});
@override
Widget build(BuildContext context) {
return Container(
child: Text(title)
);
}
}
I did used toString() method also but not sure whether it is the genuine way
如果您在 String?
上使用 toString()
,它将是 return 值(如果有的话),或者只是 "null"
字符串(如果它为空)。
您收到的错误是由 Text
小部件引起的。它知道您正在将可能包含 null
.
的内容显示为文本
UPD:请不要遵循此建议:
use Text(title!) instead of Text(title) – Mehran Ullah
这是一个非常糟糕的做法,破坏了整个空安全点。这样做意味着您坚持在可空类型变量中没有空值,这在大多数情况下可以避免并且不安全,因为如果您没有为变量提供值,可能会导致空错误,这可能会发生因为您使用的命名参数不是必需的并且没有任何初始值,所以您可以简单地不将任何内容传递给构造函数中的小部件。
UPD
Is this the recommended way? Because what if I do not want the variable to be required? – user4258194
Text
小部件需要 String
。这是一个位置参数,这意味着它是必需的。 Text widget 不能没有文本。
您有哪些选择可以在您的示例小部件中提供它?
答案开头部分显示的必需参数;
一个可选的non-nullable参数,初始值:
class OnbordingSliderTile extends StatelessWidget {
final String title;
//'Placeholder text' will be in title variable if you
// haven't provided a value for it when initializing OnbordingSliderTile
OnbordingSliderTile({this.title = 'Placeholder text'});
@override
Widget build(BuildContext context) {
return Container(
child: Text(title)
);
}
}
一个没有初始值的可空参数,在文本小部件中使用它时进行空检查:
class OnbordingSliderTile extends StatelessWidget {
final String? title;
//you can skip providing this parameter to the constructor and then it will be null
OnbordingSliderTile({this.title});
@override
Widget build(BuildContext context) {
return Container(
// You have to check if value in your variable is a String, not null
// and make sure you provided some text for the case if it null
child: Text(title ?? 'Placeholder text')
);
}
}
如果在未提供任何值时需要此文本为空,请在提供的任何选项中使用空字符串作为占位符文本。
Flutter 2.0引入null safety之后,但是在获取String变量的时候Flutter报错-
The argument type 'String?' can't be assigned to the parameter type 'String'.dart(argument_type_not_assignable)
我想要一个构造函数,如何实现带空安全的构造函数?还有如何获取字符串变量。
我也用过 toString() 方法,但不确定它是否是真正的方法。
class OnbordingSliderTile extends StatelessWidget {
final String? title;
OnbordingSliderTile({this.title});
@override
Widget build(BuildContext context) {
return Container(
//This Text widget is showing the error
child: Text(title)
);
}
}
Type String?
表示此类型的变量必须有字符串或空值。在您的情况下,您可能总是需要一个标题,并且此变量不应为空。
所以解决方案是将类型更改为 String
并将 required
关键字添加到构造函数中的命名参数。
class OnbordingSliderTile extends StatelessWidget {
final String title;
OnbordingSliderTile({required this.title});
@override
Widget build(BuildContext context) {
return Container(
child: Text(title)
);
}
}
I did used toString() method also but not sure whether it is the genuine way
如果您在 String?
上使用 toString()
,它将是 return 值(如果有的话),或者只是 "null"
字符串(如果它为空)。
您收到的错误是由 Text
小部件引起的。它知道您正在将可能包含 null
.
UPD:请不要遵循此建议:
use Text(title!) instead of Text(title) – Mehran Ullah
这是一个非常糟糕的做法,破坏了整个空安全点。这样做意味着您坚持在可空类型变量中没有空值,这在大多数情况下可以避免并且不安全,因为如果您没有为变量提供值,可能会导致空错误,这可能会发生因为您使用的命名参数不是必需的并且没有任何初始值,所以您可以简单地不将任何内容传递给构造函数中的小部件。
UPD
Is this the recommended way? Because what if I do not want the variable to be required? – user4258194
Text
小部件需要 String
。这是一个位置参数,这意味着它是必需的。 Text widget 不能没有文本。
您有哪些选择可以在您的示例小部件中提供它?
答案开头部分显示的必需参数;
一个可选的non-nullable参数,初始值:
class OnbordingSliderTile extends StatelessWidget { final String title; //'Placeholder text' will be in title variable if you // haven't provided a value for it when initializing OnbordingSliderTile OnbordingSliderTile({this.title = 'Placeholder text'}); @override Widget build(BuildContext context) { return Container( child: Text(title) ); } }
一个没有初始值的可空参数,在文本小部件中使用它时进行空检查:
class OnbordingSliderTile extends StatelessWidget { final String? title; //you can skip providing this parameter to the constructor and then it will be null OnbordingSliderTile({this.title}); @override Widget build(BuildContext context) { return Container( // You have to check if value in your variable is a String, not null // and make sure you provided some text for the case if it null child: Text(title ?? 'Placeholder text') ); } }
如果在未提供任何值时需要此文本为空,请在提供的任何选项中使用空字符串作为占位符文本。