如何在RXMAP中添加新条目

发布于 2025-02-07 17:36:31 字数 969 浏览 6 评论 0原文

通过使用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);
  }
}

我尝试在单击时在TextButton的帮助下调用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");
                }),

,执行打印语句,但仍未更新地图。如何添加新的密钥,价值对进行评论?我很困惑。

By using getX, i was trying to modify changes to RxMap. But I couldnot find the way to add value to map. add() is not defined at all for RxMap.
putIfAbsent() is not adding up new entry to RxMap.

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

I tried calling addReview with the help of TextButton as

 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");
                }),

On click, the print statement gets executed, still map is not updated. How can I add new key, value pairs to reviews? I'm confused.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

颜漓半夏 2025-02-14 17:36:31

假设我们的控制器中有以下映射:

RxMap<String, String> map = RxMap();

要在此映射中添加数据:

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

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

{hello: bye}

现在,如果我们使用putifabset:

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

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

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

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

{hello: bye, lol: bello}

为了为空的地图添加值,最好使用addall。如果您确定拥有相同的密钥,请使用Putifabsent。

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

Let's say we have the following map in our controller:

RxMap<String, String> map = RxMap();

To add data to this map we can do:

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

Now, if we print map, the following will be printed as it is added to map:

{hello: bye}

Now, if we do with putIfAbset:

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

The console print will still be the same as the key is already present in map so nothing will be added.
But if we change the above code to this:

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

Now, if we print to console we will see:

{hello: bye, lol: bello}

To add value to an empty map it's better to use addAll. Use putIfAbsent if you are sure you have same key.

Your code looks right to me as you can also add values to map your way. Just try to print to console everytime you click submit button. Probably it is saving data to your map, without Obx or rebuilds you really cannot see changes in your widget.

格子衫的從容 2025-02-14 17:36:31

您创建rxmap就像

rxmap&lt; string,string&gt;评论=&lt; string,string&gt; {}。obs;

案例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中更改外部,

请考虑您使getxController和名称是控制器

Add New Element 
controller.reviews['yourNewkey'] ="newValue";

Update existing Element 
controller.reviews.update("yourKey", (value) => "yourUpdateValue");

You create your RxMap was like

RxMap<String, String> reviews = <String, String>{}.obs;

Case 1 : Change inside 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

Case 2 : Change outside from GetXController

Consider you make GetXController and name is controller

Add New Element 
controller.reviews['yourNewkey'] ="newValue";

Update existing Element 
controller.reviews.update("yourKey", (value) => "yourUpdateValue");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文