列出getx控制器中的obs变量,在扑朔迷离中添加值之后仍然为空
当我将值添加到函数中的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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您已经声明了
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 thesetImages()
function