在ChangeNotifierProvider中使用双点(..)运算符/级联

问题描述 投票:-1回答:1
ChangeNotifierProvider(
  builder: (context) => AppStateModel()..loadBrands(),
  child: MyTestApp(),
)

为什么我们必须这样称呼AppStateModel()..loadBrands(),级联如何在这里帮助我们?

flutter dart cascading flutter-change-notifier
1个回答
1
投票

级联符号(..)

级联(..)允许您对同一对象执行一系列操作。除了函数调用,您还可以访问同一对象上的字段。这通常可以节省创建临时变量的步骤,并允许您编写更多流畅的代码。

例如,您可以使用Cascade operator,例如:

//call the method of app state class without creating a variable to hold an instance of the class
 AppStateModel()..loadBrands();

而不是创建像这样的变量:

//create an object of appstate and store in a variable
AppStateModel appState;
//access the methods of appstate class
appSate.localBrands();

要详细了解Cascade Operator,请查看以下链接:Cascade Operators

我希望这会有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.