gpt4 book ai didi

flutter - `Class._()..property` 是什么意思?

转载 作者:行者123 更新时间:2023-12-03 02:45:40 25 4
gpt4 key购买 nike

._().. 是什么意思在下面的代码中是什么意思?

class CounterState {
int counter;

CounterState._();

factory CounterState.init() {
return CounterState._()..counter = 0;
}
}

更准确地说 - 两个点是什么 ..平均过去 ._() ?

最佳答案

级联符号 (..)

Dart Languague tour ,您应该真正检查一下,因为它包含很多有用的信息,您还可以找到有关您提到的级联符号的信息:

Cascades (..) allow you to make a sequence of operations on the same object. In addition to function calls, you can also access fields on that same object. This often saves you the step of creating a temporary variable and allows you to write more fluid code.



例如,如果你想更新渲染对象的多个字段,你可以简单地使用级联符号来保存一些字符:

renderObject..color = Colors.blue
..position = Offset(x, y)
..text = greetingText
..listenable = animation;

// The above is the same as the following:
renderObject.color = Colors.blue;
renderObject.position = Offset(x, y);
renderObject.text = greetingText;
renderObject.listenable = animation;

当您想要 时,它也会有所帮助返回 与赋值或调用函数在同一行中的对象:

canvas.drawPaint(Paint()..color = Colors.amberAccent);

命名构造函数
._()named private constructor .如果一个类没有指定另一个非私有(private)的构造函数(默认的或命名的),该类 不能从库外部实例化。

class Foo {
Foo._(); // Cannot be called from outside of this file.

// Foo(); <- If this was added, the class could be instantiated, even if the private named constructor exists.
}

Learn more关于私有(private)构造函数。

关于flutter - `Class._()..property` 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59466648/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com