对于 StatelessWidget,我如何才能拥有一个带有命名参数的构造函数并在进行某些验证后设置私有(private)最终变量。
class MyWidget extends StatelessWidget {
const MyWidget({bool foo}) : /*do validation e.g. ArgumentError.checkNotNull(foo)*/ _foo = foo;
final bool _foo;
@override
Widget build(BuildContext context) {...}
}
最佳答案
试试这个:
class MyWidget extends StatelessWidget {
MyWidget({bool foo})
: assert(foo != null), // validation
_foo = foo; // assigning value
final bool _foo;
@override
Widget build(BuildContext context) {...}
}
关于flutter - 具有命名参数的构造函数设置私有(private)变量后验证,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58156862/