小编Cod*_*tel的帖子

Getx Flutter - 更新列表中的项目不是反应性的

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

家庭控制器

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;
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

模型

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"],
      );
Run Code Online (Sandbox Code Playgroud)

看法

class HomeScreen …
Run Code Online (Sandbox Code Playgroud)

dart hybrid-mobile-app flutter getx flutter-getx

5
推荐指数
2
解决办法
2762
查看次数

标签 统计

dart ×1

flutter ×1

flutter-getx ×1

getx ×1

hybrid-mobile-app ×1