【问题标题】:Getx Flutter - Updating item in list is not reactiveGetx Flutter - 更新列表中的项目不是反应性的
【发布时间】:2021-06-02 21:41:54
【问题描述】:

我使用 getx 作为我的颤振应用程序的状态管理。但是我在更新列表中的值时遇到了困难。所以我有一个参数为 isFollowing 的用户模型。当我点击一个按钮时,isFollowing 变量会改变并且颜色会被更新。但它没有发生。我使用 Obx 作为我的小部件,因为我已经在开始时注入了状态。获取数据并在前端显示它一切正常。但是当我想更改列表中的值时,它不会更新。我的最小可重现示例

HomeController

class HomeController extends GetxController {
  var userslist = List<User>().obs;

  @override
  void onInit() {
    fetchUsers();
    super.onInit();
  }

  void fetchUsers() async {
    var users= await ApiService().getapidata('${usersurl}feed');
    if (users!= null) {
      userslist.value= users;
    }
  }
}

型号

class User {
  User({
    this.id,
    this.user_name,
    this.profile_picture,
    this.isFollowing,
  });

  int id;
  String user_name;
  String profile_picture;
  bool isFollowing;


  factory User.fromJson(Map<String, dynamic> json) => User(
        id: json["id"],
        user_name: json["user_name"],
        profile_picture: json["profile_picture"],
        isFollowing: json["is_following"],
      );

查看

class HomeScreen extends StatelessWidget {
  final HomeController homeController = Get.put(HomeController());

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SingleChildScrollView(
        physics: ScrollPhysics(),
        child: Obx(
          () => ListView.builder(
            shrinkWrap: true,
            physics: NeverScrollableScrollPhysics(),
            itemCount: homeController.usersList.length,
            itemBuilder: (context, index) {
              return UserWidget(homeController.usersList[index], index);
            },
          ),
        ),
      ),
    );
  }
}

用户小部件

class UserWidget extends StatelessWidget {
  final User user;
  final int index;
  UserWidget (this.user, this.index);
  @override
  Widget build(BuildContext context) {
    return InkWell(
        onTap : ()=> user.isFollowing = true // When I click on this the container it  shall be updated to yellow
        child: Obx( () => Container(
        decoration: const BoxDecoration(color: user.isFollowing ? Colors.yellow : Colors.black ), // Here is the update I wanna make
      ))
    );
  }
}

【问题讨论】:

  • 你能把你的APIServices类的getapidata方法贴出来
  • @Coding Hotel,你找到解决方案了吗?
  • 仅将以下语句 userslist.value= users; 更改为 userlist.addAll(users)

标签: flutter dart hybrid-mobile-app flutter-getx


【解决方案1】:

在您的 fetchUsers 方法中,而不是使用

userslist.value= users;

使用

userslist.assignAll(users);

【讨论】:

  • 谢谢.. 我使用的类似 userslist.value.addAll(users) 的东西不起作用。将其更改为 assignAll 后,小部件重新渲染
【解决方案2】:

请为此使用 GetBuilder。

onTap : (){ 
    user.isFollowing = true // When I click on this the container it  shall be updated to yellow
    controller.update()
}

【讨论】:

  • 谢谢!。我有一个 List 来加载复选框列表,这个炒锅完美。
  • 或者你可以使用 user.refresh()。当模型类发生变化时,需要调用 refresh 来刷新数据
【解决方案3】:

它对我有用。对响应式列表进行更改后,添加示例:

if (users!= null) {
      userslist.value= users;

      userlist.refresh();
    } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-01-18
    • 2022-01-22
    • 2022-10-23
    • 2021-12-17
    • 2021-10-08
    • 2022-10-23
    • 2021-01-21
    • 1970-01-01
    相关资源
    最近更新 更多