【发布时间】:2021-07-16 06:54:10
【问题描述】:
我正在尝试通过这个演示项目来学习状态管理和依赖注入。我正在尝试演示在整个地方注入一些方法,就像我在程序中可能需要的那样。我使用 GetX 是因为我喜欢在非小部件类中无需上下文即可做到这一点。
所以我的问题是最后一个类中的最后一个方法 summationReturns()。尝试使用带有 return 语句的方法并将它们相加。我在两个地方称之为。在浮动按钮中,这工作正常,但在我的文本小部件中,我得到一个脏状态错误。
当其他一切都正常时,为什么这不起作用?我认为这将是最后一个问题的推论,什么是脏状态?似乎是两个问题,但我想它们是同一个问题。
///
///
/// DEMO PROJECT WORKING OUT GETX
/// WORKOUT DEPENDANCY INJECTION AND STATE MANAGEMENT
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:get/get_state_manager/get_state_manager.dart';
void main() {
runApp(GetMaterialApp(
home: Home(),
debugShowCheckedModeBanner: false,
));
}
class Home extends StatelessWidget {
// Injection of dependancy
final Controller controller = Get.put(Controller());
final Observable observable = Get.put(Observable());
final SimpleMath simpleMath = Get.put(SimpleMath());
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text('GetX Demo'),
),
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[
Text('Get builders:'),
GetBuilder<Controller>(builder: (controller) {
return Text(controller.count.toString());
}),
GetBuilder<Controller>(builder: (controller) {
return Text(controller.countList.toString());
}),
GetBuilder<Controller>(builder: (controller) {
return Text(controller.returnCount().toString());
}),
GetBuilder<Controller>(builder: (controller) {
return Text(controller.returnList().toString());
}),
SizedBox(height: 20.0),
Text('Get observables:'),
Obx(() => Text(observable.count.value.toString())),
Obx(() => Text(observable.countList.value.toString())),
Obx(() => Text(observable.returnCount().toString())),
Obx(() => Text(observable.returnList().toString())),
SizedBox(height: 20.0),
Text('Get from other class:'),
GetBuilder<SimpleMath>(builder: (simpleMath) {
return Text('Variable summation: ' + simpleMath.summationVariables().toString());
}),
GetBuilder<SimpleMath>(builder: (simpleMath) {
return Text(simpleMath.summationReturns().toString());
}),
],
),
),
floatingActionButton: FloatingActionButton(
onPressed: () {
controller.crunch();
observable.crunch();
simpleMath.summationVariables();
simpleMath.summationReturns();
},
tooltip: 'Increment',
child: Icon(Icons.add),
),
);
}
}
class Controller extends GetxController {
int count = 0;
List<int> countList = [];
void crunch() {
count += 1;
countList.add(count);
update();
}
int returnCount() {
return count;
}
List<int> returnList() {
return countList;
}
}
class Observable extends GetxController {
RxInt count = 0.obs;
Rx<RxList> countList = RxList().obs;
void crunch() {
count.value += 1;
countList.value.add(count.value);
}
int returnCount() {
return count.value;
}
List<dynamic> returnList() {
return countList.value.toList();
}
}
class SimpleMath extends GetxController {
final Controller controller = Get.find<Controller>();
final Observable observable = Get.find<Observable>();
int summationVariables() {
int sum = controller.count + observable.count.value;
update();
return sum;
}
int summationReturns() {
int sum = controller.returnCount() + observable.returnCount();
print('Summation of return values: ' + sum.toString());
update();
return sum;
}
}
错误:
══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following assertion was thrown building GetBuilder<SimpleMath>(dirty, state:
GetBuilderState<SimpleMath>#4d62d):
setState() or markNeedsBuild() called during build.
This GetBuilder<SimpleMath> widget cannot be marked as needing to build because the framework is
already in the process of building widgets. A widget can be marked as needing to be built during
the build phase only if one of its ancestors is currently building. This exception is allowed
because the framework builds parent widgets before children, which means a dirty descendant will
always be built. Otherwise, the framework might not visit this widget during this build phase.
The widget on which setState() or markNeedsBuild() was called was:
GetBuilder<SimpleMath>
The widget which was currently being built when the offending call was made was:
GetBuilder<SimpleMath>
The relevant error-causing widget was:
GetBuilder<SimpleMath>
file:///Users/robertobuttazzoni/Documents/Flutter%20Tutorials/Flutter%20Learning/getx_basics/getx_basics/lib/main.dart:57:13
【问题讨论】:
-
InsaneCat,我不确定如何应用这些答案。它们实际上看起来像同一个问题,但是它们如何应用于 GetBuilder?例如,我在 get builder 中尝试了这个,但没有运气:``` if (simpleMath.isEmpty) return widget -- else return progressIndicator``` 还有为什么其他类似的方法调用有效但这个方法无效?
标签: flutter state-management flutter-getx