如何在rxmap

问题描述 投票:0回答:3

通过使用GETX,我试图修改对RXMAP的更改。但是我无法找到为映射添加价值的方法。 add()根本没有为RxMAP定义。 putifabsent()没有将新的条目添加到rxmap。

class RestaurantController extends GetxController { RxMap<String, String> reviews = <String, String>{}.obs; addReview(String name, String review) { reviews.putIfAbsent(name, () => review); } }
我尝试在textbuttonas

的帮助下调用addreview

TextButton( style: ButtonStyle( elevation: MaterialStateProperty.all<double>(10.0), backgroundColor: MaterialStateProperty.all<Color>(Colors.orange)), child: const Text("Submit"), onPressed: () { restaurantController.addReview( nameController.text, reviewController.text); print("submitted"); }),
单击,执行打印语句,仍未更新地图。如何添加新的密钥,价值对进行评论?我很困惑
    

llet说我们在控制器中有以下映射:

flutter get flutter-layout flutter-dependencies state-management
3个回答
2
投票

要在此地图中添加数据:

_controller.map.addAll({'hello': 'bye'});

现在,如果我们打印地图,则将在添加到映射时打印以下内容:

{hello: bye}

现在,如果我们使用putifabset:

_controller.map.putIfAbsent('hello', () => 'bello');

控制台打印仍将与已在地图中已经存在的密钥相同,因此不会添加任何内容。 但是,如果我们将上述代码更改为此:

_controller.map.putIfAbsent('lol', () => 'bello');

现在,如果我们打印到控制台,我们会看到:

{hello: bye, lol: bello}

要添加值为空的地图,最好使用addall。如果您确定有相同的键,请使用putifabsent。

您的代码对我来说很正确,因为您还可以添加值以映射您的方式。每当您单击提交按钮时,只需尝试打印即可安装。可能它将数据保存到您的地图,而没有OBX或重建,您确实看不到小部件中的更改。

您创建您的rxmap喜欢

RxMap

评论=

0
投票

案例1:更改getxController

Add new Element reviews['yourNewkey'] = 'newValue'; // Map <String,String> Update existing Element reviews.update("yourKey", (_) => "yourUpdateValue"); // Here with help of key (existing key) you update your value 案例2:从getxController tossiss您制作getxController,名称是Controller

Add New Element controller.reviews['yourNewkey'] ="newValue"; Update existing Element controller.reviews.update("yourKey", (value) => "yourUpdateValue");


对于我来说,在控制器中,我刚刚在添加诸如That-

之类的新值之后手动调用

update()

方法
void onChangeCity(int cityId) {
  orderFilterRequest["city_id"] = cityId;
  update();
}

0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.