ChangeNotifierProvider(
builder: (context) => AppStateModel()..loadBrands(),
child: MyTestApp(),
)
为什么我们必须这样称呼AppStateModel()..loadBrands()
,级联如何在这里帮助我们?
级联符号(..)
级联(..)允许您对同一对象执行一系列操作。除了函数调用,您还可以访问同一对象上的字段。这通常可以节省创建临时变量的步骤,并允许您编写更多流畅的代码。
例如,您可以使用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
我希望这会有所帮助。