将新对象插入列表后,如何更新ListView?

问题描述

我试图使用GetBuilder来更新状态,但是不起作用。 如果执行热重装,则新对象在列表中可见,但是页面中没有新项目。我应该重新加载页面还是应该使用反应式Observable? 另外,当我尝试通过foodList.obs使食物列表可观察时-我收到错误消息,因为我无法使食物类型对象可观察。很奇怪。

class FoodsController extends GetxController {
  List<Food> foodList = [];
  int nrOfFoods;

  onInit() {
    foodList.add(Food(name: 'banana',type: 'fruits'));
    foodList.add(Food(name: 'apples',type: 'fruits'));
    foodList.add(Food(name: 'melon',type: 'fruits'));
    super.onInit();
  }

  @override
  void onReady() {
    nrOfFoods.obs;
    super.onReady();
  }
  void addItem(Food item) {
    foodList.add(item);
    nrOfFoods = foodList.length;
    update();
  }
}

class HomePage extends StatelessWidget {
  FoodsController foodsController = Get.put(FoodsController());
  @override
  Widget build(BuildContext context) {
    print(foodsController.foodList);
    return Scaffold(
      appBar: AppBar(
        title: Text('App Bar'),),body: Column(
        children: [
          Expanded(
            child: GetBuilder<FoodsController>(
              init: FoodsController(),builder: (_) => ListView.builder(
                itemCount: foodsController.foodList.length,itemBuilder: (BuildContext context,int index) {
                  return ListTile(
                      title: Text(foodsController.foodList[index].name));
                },FloatingActionButton(
            child: Text("test"),onPressed: () => {
              foodsController.foodList.add(Food(name: "chicken",type: "meat")),Get.to(SecondPage())
            },],);
  }
}

谢谢

解决方法

我找到了解决方案。我将控制器移到了第二页,现在可以了,尽管我认为我应该使用Obx而不是GetBuilder来使此响应。

class Food {
  Food({this.name,this.type});
  String name,type;

  @override
  String toString() => '${this.name} is a ${this.type}';
}

class FoodsController extends GetxController {
  final List<Food> foodList = [];
  int nrOfFoods = 0;

  onInit() {
    foodList.add(Food(name: 'banana',type: 'fruits'));
    foodList.add(Food(name: 'apples',type: 'fruits'));
    foodList.add(Food(name: 'melon',type: 'fruits'));
    super.onInit();
  }

  void addItem(Food item) {
    foodList.add(item);
    nrOfFoods = foodList.length;
    update();
  }
}

class HomePage extends StatelessWidget {
  final FoodsController foodsController = Get.put(FoodsController());

  @override
  Widget build(BuildContext context) {
    print(foodsController.foodList);
    return Scaffold(
      appBar: AppBar(
        title: Text('App Bar'),),body: Column(
        children: [
          FloatingActionButton(
            child: Text("test"),onPressed: () {
              foodsController.foodList.add(Food(name: "chicken",type: "meat"));
              Get.to(SecondPage());
            },],);
  }
}

class SecondPage extends StatelessWidget {
  final FoodsController ctrl = Get.find();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('App Bar'),body: Column(
        children: [
          Expanded(
            child: GetBuilder<FoodsController>(
              builder: (_) => ListView.builder(
                itemCount: ctrl.foodList.length,itemBuilder: (BuildContext context,int index) {
                  return ListTile(title: Text(ctrl.foodList[index].name));
                },FloatingActionButton(
            child: Text("test"),onPressed: () {
              ctrl.foodList.add(Food(name: "chicken",);
  }
}

谢谢

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...