Rob*_*bbB 6 state-management flutter flutter-getx
我正在尝试通过这个演示项目学习状态管理和依赖注入。我正在尝试演示在整个地方注入一些方法,就像我的程序中可能需要的那样。我使用 GetX 是因为我喜欢能够在非小部件类中无需上下文即可执行此操作。
\n所以我的问题是下面最后一个类中的最后一个方法 summationReturns() 。尝试采用带有 return 语句的方法并将它们添加在一起。我在两个地方这样称呼它。在浮动按钮中,这工作正常,但在我的文本小部件中,我收到脏状态错误。
\n为什么当其他一切都有效时这不起作用?我认为这将是最后一个问题的必然结果,什么是脏状态?看起来像是两个问题,但我想它们是同一个问题。
\n///\n///\n/// DEMO PROJECT WORKING OUT GETX\n/// WORKOUT DEPENDANCY INJECTION AND STATE MANAGEMENT\n\nimport 'package:flutter/material.dart';\nimport 'package:get/get.dart';\nimport 'package:get/get_state_manager/get_state_manager.dart';\n\nvoid main() {\n runApp(GetMaterialApp(\n home: Home(),\n debugShowCheckedModeBanner: false,\n ));\n}\n\nclass Home extends StatelessWidget {\n // Injection of dependancy\n final Controller controller = Get.put(Controller());\n final Observable observable = Get.put(Observable());\n final SimpleMath simpleMath = Get.put(SimpleMath());\n\n @override\n Widget build(BuildContext context) {\n return Scaffold(\n appBar: AppBar(\n title: Text('GetX Demo'),\n ),\n body: Center(\n child: Column(\n mainAxisAlignment: MainAxisAlignment.center,\n children: <Widget>[\n Text('Get builders:'),\n GetBuilder<Controller>(builder: (controller) {\n return Text(controller.count.toString());\n }),\n GetBuilder<Controller>(builder: (controller) {\n return Text(controller.countList.toString());\n }),\n GetBuilder<Controller>(builder: (controller) {\n return Text(controller.returnCount().toString());\n }),\n GetBuilder<Controller>(builder: (controller) {\n return Text(controller.returnList().toString());\n }),\n SizedBox(height: 20.0),\n Text('Get observables:'),\n Obx(() => Text(observable.count.value.toString())),\n Obx(() => Text(observable.countList.value.toString())),\n Obx(() => Text(observable.returnCount().toString())),\n Obx(() => Text(observable.returnList().toString())),\n SizedBox(height: 20.0),\n Text('Get from other class:'),\n GetBuilder<SimpleMath>(builder: (simpleMath) {\n return Text('Variable summation: ' + simpleMath.summationVariables().toString());\n }),\n GetBuilder<SimpleMath>(builder: (simpleMath) {\n return Text(simpleMath.summationReturns().toString());\n }),\n ],\n ),\n ),\n floatingActionButton: FloatingActionButton(\n onPressed: () {\n controller.crunch();\n observable.crunch();\n simpleMath.summationVariables();\n simpleMath.summationReturns();\n },\n tooltip: 'Increment',\n child: Icon(Icons.add),\n ),\n );\n }\n}\n\nclass Controller extends GetxController {\n int count = 0;\n List<int> countList = [];\n\n void crunch() {\n count += 1;\n countList.add(count);\n update();\n }\n\n int returnCount() {\n return count;\n }\n\n List<int> returnList() {\n return countList;\n }\n}\n\nclass Observable extends GetxController {\n RxInt count = 0.obs;\n Rx<RxList> countList = RxList().obs;\n\n void crunch() {\n count.value += 1;\n countList.value.add(count.value);\n }\n\n int returnCount() {\n return count.value;\n }\n\n List<dynamic> returnList() {\n return countList.value.toList();\n }\n}\n\nclass SimpleMath extends GetxController {\n final Controller controller = Get.find<Controller>();\n final Observable observable = Get.find<Observable>();\n\n int summationVariables() {\n int sum = controller.count + observable.count.value;\n update();\n return sum;\n }\n\n int summationReturns() {\n int sum = controller.returnCount() + observable.returnCount();\n print('Summation of return values: ' + sum.toString());\n update();\n return sum;\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n错误:
\n\xe2\x95\x90\xe2\x95\x90\xe2\x95\xa1 EXCEPTION CAUGHT BY WIDGETS LIBRARY \xe2\x95\x9e\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\xe2\x95\x90\nThe following assertion was thrown building GetBuilder<SimpleMath>(dirty, state:\nGetBuilderState<SimpleMath>#4d62d):\nsetState() or markNeedsBuild() called during build.\nThis GetBuilder<SimpleMath> widget cannot be marked as needing to build because the framework is\nalready in the process of building widgets. A widget can be marked as needing to be built during\nthe build phase only if one of its ancestors is currently building. This exception is allowed\nbecause the framework builds parent widgets before children, which means a dirty descendant will\nalways be built. Otherwise, the framework might not visit this widget during this build phase.\nThe widget on which setState() or markNeedsBuild() was called was:\n GetBuilder<SimpleMath>\nThe widget which was currently being built when the offending call was made was:\n GetBuilder<SimpleMath>\n\nThe relevant error-causing widget was:\n GetBuilder<SimpleMath>\n file:///Users/robertobuttazzoni/Documents/Flutter%20Tutorials/Flutter%20Learning/getx_basics/getx_basics/lib/main.dart:57:13\nRun Code Online (Sandbox Code Playgroud)\n
在构建过程中调用update是脏场景的一个示例。要解决您的问题,请勿update在GetBuilder.
样本...
在家
GetBuilder<SimpleMath>(
builder: (simpleMath) => Text('Variable summation: ' +
simpleMath
.summationVariables(shouldUpdate: false)
.toString())),
GetBuilder<SimpleMath>(
builder: (simpleMath) => Text(simpleMath
.summationReturns(shouldUpdate: false)
.toString())),
Run Code Online (Sandbox Code Playgroud)
在简单数学中
int summationVariables({bool shouldUpdate = true}) {
int sum = controller.count + observable.count.value;
if (shouldUpdate) update();
return sum;
}
int summationReturns({bool shouldUpdate = true}) {
int sum = controller.returnCount() + observable.returnCount();
print('Summation of return values: ' + sum.toString());
if (shouldUpdate) update();
return sum;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
6601 次 |
| 最近记录: |