列出getx控制器中的obs变量,在扑朔迷离中添加值之后仍然为空

发布于 2025-02-12 19:01:11 字数 1475 浏览 0 评论 0原文

当我将值添加到函数中的GETX控制器中的obs列表中时,它显示了成功添加数据的长度。但是,当我在另一个函数中调用变量时,列表仍然为空。

class ExampleController extends GetxController {
  var dataList = <dynamic>[].obs;

  void setImages(items) {
    dataList.addAll(items);
    log(dataList.length.toString()); // shows the data length after added items
  }

  void onButtonPressed() {
    log(dataList.length.toString()); // shows 0 length
  }
}

我把像这样的控制器放置了..

void main {
  runApp(const myApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.put(ExampleController());

    return GetMaterialApp(
      title: 'GetX Example',
      debugShowCheckedModeBanner: false,
      home: const SplashScreen(),
    );
  }
}

这就是我实例化控制器的方式。

class DataPage extends StatefulWidget {
  const DataPage({Key? key}) : super(key: key);

  @override
  State<DataPage> createState() => _DataPageState();
}

class _DataPageState extends State<DataPage> {
  final _exampleController = Get.find<ExampleController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Center(
        child: CustomButtonIcon(
          label: 'Get Data',
          onPressed: () => _exampleController.onButtonPressed(),
          icon: Icons.arrow_back_rounded,
        ),
      )),
    );
  }
}

When i add value to an obs list in GetX controller inside a function, it shows the length that the data is successfully added. But when i call the variable in another function, the list is still empty.

class ExampleController extends GetxController {
  var dataList = <dynamic>[].obs;

  void setImages(items) {
    dataList.addAll(items);
    log(dataList.length.toString()); // shows the data length after added items
  }

  void onButtonPressed() {
    log(dataList.length.toString()); // shows 0 length
  }
}

I put the controller like this ..

void main {
  runApp(const myApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.put(ExampleController());

    return GetMaterialApp(
      title: 'GetX Example',
      debugShowCheckedModeBanner: false,
      home: const SplashScreen(),
    );
  }
}

And this is how i instantiate the controller..

class DataPage extends StatefulWidget {
  const DataPage({Key? key}) : super(key: key);

  @override
  State<DataPage> createState() => _DataPageState();
}

class _DataPageState extends State<DataPage> {
  final _exampleController = Get.find<ExampleController>();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
          child: Center(
        child: CustomButtonIcon(
          label: 'Get Data',
          onPressed: () => _exampleController.onButtonPressed(),
          icon: Icons.arrow_back_rounded,
        ),
      )),
    );
  }
}

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

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

发布评论

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

评论(2

蓝眸 2025-02-19 19:01:11

您已经声明了setImages()函数。但从未打电话。

因此,控制器中的列表obs变量为空。

按预期工作。
ontbuttonpresse()函数函数之前调用setImages()函数

You have declared the setImages() function . But never called .

So the List obs variable in Controller is empty.

To work as expected.
Before the onButtonPressed() function call the setImages() function

演出会有结束 2025-02-19 19:01:11
 void setImages(items) {
    dataList.addAll(items);
    log(dataList.length.toString()); // shows the data length after added items

     update(); // use this
  }

  void onButtonPressed() {

Future.delayed(const Duration(milliseconds: 500), () {
    log(dataList.length.toString()); // shows 0 length

});
  }
 void setImages(items) {
    dataList.addAll(items);
    log(dataList.length.toString()); // shows the data length after added items

     update(); // use this
  }

  void onButtonPressed() {

Future.delayed(const Duration(milliseconds: 500), () {
    log(dataList.length.toString()); // shows 0 length

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