Flutter 中的 "dirty" 是什么以及导致此 "dirty" 状态的原因是什么?

What is "dirty" in Flutter & what is causing this "dirty" state?

我正在尝试通过这个演示项目学习状态管理和依赖注入。我正在尝试演示在整个地方注入一些方法,就像我在我的程序中可能需要的那样。我正在使用 GetX,因为我喜欢能够在非小部件 classes.

的情况下在没有上下文的情况下执行此操作

所以我这里的问题是下面最后 class 中的最后一个方法 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

在构建过程中调用 update 是肮脏场景的一个例子。要解决您的问题,请不要在 GetBuilder.

中调用 update

示例...

在家

GetBuilder<SimpleMath>(
    builder: (simpleMath) => Text('Variable summation: ' +
        simpleMath
            .summationVariables(shouldUpdate: false)
            .toString())),
GetBuilder<SimpleMath>(
    builder: (simpleMath) => Text(simpleMath
        .summationReturns(shouldUpdate: false)
        .toString())),

在简单数学中

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;
}