【问题标题】:query element of a list of maps, and update one element in dart查询地图列表的元素,并更新 dart 中的一个元素
【发布时间】:2020-08-31 16:03:38
【问题描述】:

我在 Dart 上有一张地图,其结构如下

[
  {
   "id": 'asdasy21dh',
    "price": 23,
     "quantity": 2
  },
 {
   "id": 'asd2d21aa',
    "price": 43,
     "quantity": 1
  },
]

我需要增加列表元素中的数量,id 为:asd2d21aa,我该怎么做?因为它是一个购物车,产品的数量大大增加。非常感谢。

【问题讨论】:

    标签: list flutter dart hashmap


    【解决方案1】:

    您可以在下面复制粘贴运行完整代码
    您可以使用firstWhere 并检查id

    代码sn-p

    var target = cartList.firstWhere((item) => item["id"] == 'asd2d21aa');
    if (target != null) {
        target["quantity"] = target["quantity"] + 1;
    }
    

    输出

    I/flutter (17317): {id: asd2d21aa, price: 43, quantity: 2}
    

    完整代码

    import 'package:flutter/material.dart';
    
    void main() {
      runApp(MyApp());
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          title: 'Flutter Demo',
          theme: ThemeData(
            primarySwatch: Colors.blue,
            visualDensity: VisualDensity.adaptivePlatformDensity,
          ),
          home: MyHomePage(title: 'Flutter Demo Home Page'),
        );
      }
    }
    
    class MyHomePage extends StatefulWidget {
      MyHomePage({Key key, this.title}) : super(key: key);
    
      final String title;
    
      @override
      _MyHomePageState createState() => _MyHomePageState();
    }
    
    class _MyHomePageState extends State<MyHomePage> {
      int _counter = 0;
      List<Map> cartList = [
        {"id": 'asdasy21dh', "price": 23, "quantity": 2},
        {"id": 'asd2d21aa', "price": 43, "quantity": 1},
      ];
    
      void _incrementCounter() {
        var target = cartList.firstWhere((item) => item["id"] == 'asd2d21aa');
        if (target != null) {
          target["quantity"] = target["quantity"] + 1;
        }
        print(cartList[1].toString());
    
        setState(() {
          _counter++;
        });
      }
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text(widget.title),
          ),
          body: Center(
            child: Column(
              mainAxisAlignment: MainAxisAlignment.center,
              children: <Widget>[
                Text(
                  'You have pushed the button this many times:',
                ),
                Text(
                  '$_counter',
                  style: Theme.of(context).textTheme.headline4,
                ),
              ],
            ),
          ),
          floatingActionButton: FloatingActionButton(
            onPressed: _incrementCounter,
            tooltip: 'Increment',
            child: Icon(Icons.add),
          ),
        );
      }
    }
    

    【讨论】:

    • chunhunghan - 它适用于一个元素,但如果我们想用target2 映射更新target,更新后我们如何更新cartList?仅供参考target2 具有相同的地图结构,但某些元素值已更改。请建议我们如何做到这一点?非常感谢。
    猜你喜欢
    • 2019-02-12
    • 2023-03-27
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-10-15
    相关资源
    最近更新 更多