Flutter 常见警告

1、Avoid empty catch blocks.
如果try catch 中的catch没有任何实现则需要把catch的e异常改为_即可消除警告

try {
  ...
} catch (e) {}

更改为:

try {
   ...
} catch (_) {}

2、Prefer const with constant constructors.
意思是需要加上const常量修饰符

EdgeInsets.all(0),
更改为:
const EdgeInsets.all(0),

3、Prefer using if null operators.
发生在使用三目运算符当中

widget.btnHeight == null
          ? ScreenUtil.getInstance().getHeight(48.0)
          : widget.btnHeight,
更改为:
widget.btnHeight ?? ScreenUtil.getInstance().getHeight(48.0),

4、The '!' will have no effect because the receiver can't be null.
在定义空安全之后的使用中

double? _twidth = (widget.textWidth ?? ScreenUtil.getInstance().getWidth(280.0));
double _spadding = (_swidth - _twidth!) / 2;
更改为:
double? _twidth = (widget.textWidth ?? ScreenUtil.getInstance().getWidth(280.0));
double _spadding = (_swidth - _twidth) / 2;

5、Don't access members with this unless avoiding shadowing.
引用了this关键字

this.widget.clickOnTap
更改为:
widget.clickOnTap

6、SizedBox for whitespace.
使用Container布局

Container()
更改为:
SizedBox()

6、Unnecessary new keyword.
创建布局控件时使用new关键字

new Text()
更改为:
Text()

7、Avoid using braces in interpolation when not needed.
日志输出的时候对于单独的变量使用了大括号

print("scanEndHandler-->${isScanning}");
更改为:
print("scanEndHandler-->$isScanning");

8、Use key in widget constructors.
定义的StatefulWidget 中没有添加构造方法

const GuidePage({Key? key}) : super(key: key);

9、The method doesn't override an inherited method.
定义方法之前添加了@override

@override
Widget A(){
}
更改为
Widget A(){
}
删除@override
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容