带有const关键字的Flutter变量 [英] Flutter variable with const keyword

查看:137
本文介绍了带有const关键字的Flutter变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在Flutter文档的编写您的第一个应用程序教程的第4步中,名为第4步:创建无限滚动ListView,要求您创建2个变量,显示在此处:

In the 'Write Your First App' tutorial on the Flutter docs, Step 4 titled "Step 4: Create an infinite scrolling ListView", you are asked to create 2 variables show here:

class RandomWordsState extends State<RandomWords> {
  final _suggestions = <WordPair>[];

  final _biggerFont = const TextStyle(fontSize: 18.0);
  ...
}

为什么第3行中使用了const 关键字?我来自C#和JavaScript背景,我不习惯在赋值语句的右侧看到它。我注意到如果我将其删除,它仍然可以按预期工作。您能否以细语解释 ,为什么要使用它,我什么时候应该这样做?我猜想这太过分了,我不必使用它,但我只是想确定一下。

Why is the const keyword used in the 3rd line? I come from a C# and JavaScript background and I'm not used to seeing this on the right side of an assignment statement. I notice if I remove it it still works as I expected. Could you please explain in lamens terms why this is being used and when should I do the same? I'm guessing it's overkill and I don't have to use it but I just want to make sure.

我不认为这是重复的,因为此帖子中的答案非常适合解释我的问题,在其他帖子中找不到,更不用说其他帖子了

I do not believe this is a duplicate because the answer in this post was perfect in explaining my question and not found in the other post, not to mention the other post is a two part question that nobody would find when using google.

推荐答案

来自dart新闻网站:

From dart news website:


const 的含义在Dart中更加复杂和微妙。
const修改。您可以在创建集合时使用它,如 const [1、2、3] 之类的
,以及在构造诸如(而不是新的)
之类的对象时(如 const Point(2,3)。在这里,const表示可以完全在编译时确定对象的整个
深度状态,并且
对象将被冻结并且完全不可变。

"const" has a meaning that's a bit more complex and subtle in Dart. const modifies values. You can use it when creating collections, like const [1, 2, 3], and when constructing objects (instead of new) like const Point(2, 3). Here, const means that the object's entire deep state can be determined entirely at compile time and that the object will be frozen and completely immutable.

在此处了解更多信息。

用我的话来说,您可以使用 const 构造函数(定义为<$ c $的构造函数c> const ),例如 const Text() new Text()

In my words, you can use const constructor(constructors that defined as const) like const Text() or new Text().

如果使用 const Text():这将仅分配一个内存空间,而当您添加另一个 const Text(),它将重用同一对象,但 new Text()将始终分配新的内存空间。因此,使用 const 可以提高程序性能(不是那么多的性能,而是更少的内存分配)。另外,如果需要重用,则可以将类构造函数定义为 const

If you use const Text(): This will allocate only one memory space and when you are add another const Text(), that will reuse the same object but the new Text() will always allocate new memory space. So, using const you can increase your program performance(not that much performance but less memory allocation). Also, you can define your class constructors as const, if it need to reuse.


我注意到,如果我删除它,它仍然可以按预期运行。

I notice if I remove it it still works as I expected.

那是因为使用Dart的两个 new const 关键字在创建对象/实例时是可选的,将由Dart VM处理。最初存在一些问题,但现在已解决。

That because with Dart two new and const keyword optional when you are creating object/instance, that will handle by the Dart VM. Initially there was some problems but those are fixed now.

即使您避免使用 const / 这些将由 Dart VM 。使这两个关键字为可选的原因是在Flutter中,您必须在各处键入这两个关键字(例如:在小部件树中)。

Even if you avoid const/new those will added by the Dart VM. The reason for to made those two keyword optional is in Flutter you have to type those two everywhere(ex: in widget tree).

第二次飞镖增强功能()2月23日

这篇关于带有const关键字的Flutter变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆