【发布时间】:2022-06-12 16:41:49
【问题描述】:
我有一个嵌套的 GetX 控制器配置,它们代表我的数据模型。它们看起来像这样(我缩短了代码以仅显示控制器结构):
class AppController extends GetxController {
final package = PackageController().obs;
void openPackage() {
// some code with unzipping, parsing and eventually creating instance of PackageController
package.value = packageController;
}
}
.
class PackageController extends GetxController {
final rounds = RxList<RoundController>();
void addRound() {
rounds.add(RoundController());
}
void deleteRound(int index) {
rounds.removeAt(index);
}
}
.
class RoundController extends GetxController {
final themes = RxList<ThemeController>();
void addTheme() {
themes.add(ThemeController());
}
void deleteTheme(int index) {
themes.removeAt(index);
}
}
它更深入,但这足以理解。在我的 UI 小部件中,我使用 final store = Get.put(AppController()); 访问 AppController,并通过它访问我需要的任何嵌套控制器。
现在问题来了:像onInit() 这样的生命周期方法只被AppController() 调用,而不是任何嵌套的方法。那么,我需要知道一些技巧吗,或者我以错误的方式使用 GetX,或者是什么?
【问题讨论】:
标签: flutter flutter-getx