简体   繁体   English

不可变的 flutter 小部件如何存储其可变状态?

[英]How do immutable flutter widgets store their mutable states?

Warning: Prefer const with constant constructors警告: Prefer const with constant constructors

cause: - Text("dd")原因: - 文本(“dd”)
solution: - const Text("dd")解决方案: - const Text("dd")

StatefulWidget instances themselves are immutable and store their
mutable state either in separate State objects that are created by the createState 
method, or in objects to which that State subscribes, for example Stream or 
ChangeNotifier objects, to which references are stored in final fields on the 
StatefulWidget itself.

Sources:资料来源:
https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html https://api.flutter.dev/flutter/widgets/StatefulWidget-class.html
https://stackoverflow.com/a/58136993/462608 https://stackoverflow.com/a/58136993/462608

Please give examples to show how do widgets store their mutable states by createState method or in objects to which that State subscribes.请举例说明小部件如何通过createState方法或在 State 订阅的对象中存储其可变状态。

Example:例子:
Which field here is considered mutable ?这里的哪个字段被认为是mutable的?

class CategoryScreen extends StatefulWidget {
  String productId;

  CategoryScreen(this.productId);

  @override
  CategoryScreenState createState() => CategoryScreenState();
}

class CategoryScreenState extends State<CategoryScreen> {
  List<Item> _data = [];

  CategoryController controller = Get.put(CategoryController());

  setState(() {
     controller.selectedCategoryIndex.value = panelIndex;
  });
}

Generally you want the fields in your StatefulWidget object to be final so instead of String productId write final String productId .通常,您希望 StatefulWidget object 中的字段是final的,因此而不是String productIdfinal String productId You can reference those fields from the State object using the widget property, eg widget.productId .您可以使用widget属性(例如widget.productId )从State object 引用这些字段。 Fields in the State object can be mutable, and in your example both _data and controller are technically mutable. State object 中的字段可以是可变的,在您的示例中, controller _data技术上都是可变的。 Typically you would initialize that data in initState , so you might for instance use the controller to fill the _data list using an API call, then call setState to trigger the build of the widget.通常,您会在initState中初始化该数据,因此您可以使用_data使用controller调用填充 _data 列表,然后调用setState来触发小部件的构建。 That's what makes this widget 'Stateful'.这就是使这个小部件“有状态”的原因。 Stateless widget don't hold mutable state (eg the Text widget has no mutable state, just the text you create it with and it won't change).无状态小部件不包含可变的 state(例如, Text小部件没有可变的 state,只是您创建它的文本,它不会改变)。 Note that Stateless does not mean that a widget cannot change.请注意,无状态并不意味着小部件不能更改。 For example, a stateless widget can listen to a data stream and adjust its representation accordingly - but it won't hold state.例如,无状态小部件可以监听数据 stream 并相应地调整其表示 - 但它不会保存 state。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM