前往小程序,Get更优阅读体验!
立即前往
发布
社区首页 >专栏 >flutter开发中Use ‘const’ with the constructor to improve performance. Try adding th

flutter开发中Use ‘const’ with the constructor to improve performance. Try adding th

原创
作者头像
卓伊凡
发布2025-01-23 15:10:57
发布2025-01-23 15:10:57
4100
代码可运行
举报
文章被收录于专栏:其他相关技术其他相关技术
运行总次数:0
代码可运行

flutter开发中Use ‘const’ with the constructor to improve performance. Try adding the ‘const’ keyword to the constructor invocation.报错如何解决-优雅草卓伊凡

问题背景

flutter开发中Use ‘const’ with the constructor to improve performance. Try adding the ‘const’ keyword to the constructor invocation.

一个关于使用const关键字的警告。这个警告表示你可以在一些地方使用const关键字来提高性能。在Flutter中,如果一个小部件的所有参数都是常量,那么将其声明为const可以让Flutter更高效地重用它。

解决方案

可以在以下地方添加const关键字来解决这个警告:

诸如:

DropdownButtonFormField 的 items 参数

TextField 构造函数

InputDecoration 构造函数

Text 构造函数

等等

做个例子:

代码语言:javascript
代码运行次数:0
复制
Text(
              "Please register your account",
              style: TextStyle(
                color: Colors.white,
                fontSize: 32,
                fontFamily: "PingFang SC",
                fontWeight: FontWeight.w800,
              ),

这样的代码 flutter就会提示警告

代码语言:javascript
代码运行次数:0
复制
const Text(
              "Please register your account",
              style: TextStyle(
                color: Colors.white,
                fontSize: 32,
                fontFamily: "PingFang SC",
                fontWeight: FontWeight.w800,
              ),

如果是这样,这个问题就能解决-ok 完美

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • flutter开发中Use ‘const’ with the constructor to improve performance. Try adding the ‘const’ keyword to the constructor invocation.报错如何解决-优雅草卓伊凡
  • 问题背景
  • 解决方案
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档