无法使用getx颤动来发起列表

发布于 2025-02-11 04:18:05 字数 1316 浏览 1 评论 0原文

我有一个列表,我想通过从数据库中获取数据来启动它。出现此错误消息

[GET]已检测到GetX的不当使用。 您只能将GETX或OBX用于将要更新的特定小部件。 如果您看到此错误,您可能没有将任何可观察的变量插入GetX/obx 或将它们插入getx认为适合更新的范围之外 (示例:getx => heidewidget => variable observable)。 如果您需要更新父窗口小部件和子小部件,请将每个小部件包装在OBX/GETX中。

class PasswordController extends GetxController {
   final passwordsList = <Password>[].obs;

   final db = Dao.instance;

Future<void> initilizePasswordsList() async {
   final passwords = await db.getAllPasswords();
   passwordsList.addAll([...passwords]);
   return;
}
}
    Future<void> main() async {
    final passwordController = Get.put(PasswordController());
    await passwordController.initilizePasswordsList();

    runApp(const MyApp());
    }
  const PasswordsList({super.key});

  @override
  Widget build(BuildContext context) {
    return GetX<PasswordController>(
      builder: (controller) {
        return ListView(
          children: controller.passwordsList
              .map((e) => Obx(
                    () => PasswordTile(
                      password: e.password,                     
                      websiteName: e.websiteName,
                    ),
                  ))
              .toList(),
        );
      },
    );
}
}

I have a list and I want to initilize it by getting the data from the database. and this error message appears

[Get] the improper use of a GetX has been detected.
You should only use GetX or Obx for the specific widget that will be updated.
If you are seeing this error, you probably did not insert any observable variables into GetX/Obx
or insert them outside the scope that GetX considers suitable for an update
(example: GetX => HeavyWidget => variableObservable).
If you need to update a parent widget and a child widget, wrap each one in an Obx/GetX.

class PasswordController extends GetxController {
   final passwordsList = <Password>[].obs;

   final db = Dao.instance;

Future<void> initilizePasswordsList() async {
   final passwords = await db.getAllPasswords();
   passwordsList.addAll([...passwords]);
   return;
}
}
    Future<void> main() async {
    final passwordController = Get.put(PasswordController());
    await passwordController.initilizePasswordsList();

    runApp(const MyApp());
    }
  const PasswordsList({super.key});

  @override
  Widget build(BuildContext context) {
    return GetX<PasswordController>(
      builder: (controller) {
        return ListView(
          children: controller.passwordsList
              .map((e) => Obx(
                    () => PasswordTile(
                      password: e.password,                     
                      websiteName: e.websiteName,
                    ),
                  ))
              .toList(),
        );
      },
    );
}
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

榕城若虚 2025-02-18 04:18:05

listView中删除obx e 是不可观察的,password> passwordlist是。您已经在使用getx观察它。因此,您的listView应该看起来像:

ListView(
      children: controller.passwordsList
          .map((e) => PasswordTile(
                  password: e.password,                     
                  websiteName: e.websiteName,
                ),
              )
          .toList(),
    );

Remove the Obx inside the ListView as e is not observable, passwordList is. And you are already observing it with GetX. Therefore your ListView should look like:

ListView(
      children: controller.passwordsList
          .map((e) => PasswordTile(
                  password: e.password,                     
                  websiteName: e.websiteName,
                ),
              )
          .toList(),
    );
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文