赞
踩
自定义Widget的空安全适配分两种情况:
对于自定的Widget无论是页面的某控件还是整个页面,通常都会为Widget定义一些属性。在进行空安全适配时要对属性进行一下分类:
?
进行修饰required
进行修饰- class WebView extends StatefulWidget {
- String? url;
- final String? statusBarColor;
- final String? title;
- final bool? hideAppBar;
- final bool backForbid;
-
- WebView(
- {this.url,
- this.statusBarColor,
- this.title,
- this.hideAppBar,
- this.backForbid = false})
- ...
State的空安全适配主要是根据它的成员变量是否可空进行分类:
?
进行修饰late
修饰为延时变量- class _TravelPageState extends State<TravelPage> with TickerProviderStateMixin {
- late TabController _controller; //延时初始
- List<TravelTab> tabs = []; //定义时初始化
- ...
- @override
- void initState() {
- super.initState();
- _controller = TabController(length: 0, vsync: this);
- ...
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。