List 查询地图列表的元素,并更新dart中的一个元素
我在省道上有一张结构如下的地图List 查询地图列表的元素,并更新dart中的一个元素,list,flutter,dart,hashmap,List,Flutter,Dart,Hashmap,我在省道上有一张结构如下的地图 [ { "id": 'asdasy21dh', "price": 23, "quantity": 2 }, { "id": 'asd2d21aa', "price": 43, "quantity": 1 }, ] 我需要增加id为asd21aa的列表元素中的数量,我如何才能做到这一点?因为它是一辆购物车,产品数量大幅增加。非常感谢。您可以复制粘贴运行下面的完整代码 您可以使用firstWhere并检查
[
{
"id": 'asdasy21dh',
"price": 23,
"quantity": 2
},
{
"id": 'asd2d21aa',
"price": 43,
"quantity": 1
},
]
我需要增加id为asd21aa的列表元素中的数量,我如何才能做到这一点?因为它是一辆购物车,产品数量大幅增加。非常感谢。您可以复制粘贴运行下面的完整代码
您可以使用
firstWhere
并检查id
代码片段
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),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
列表cartList=[
{“id”:“asdasy21dh”,“价格”:23,“数量”:2},
{“id”:“asd21aa”,“价格”:43,“数量”:1},
];
void _incrementCounter(){
var target=cartList.firstWhere((项)=>item[“id”]==“asd21aa”);
如果(目标!=null){
目标[“数量”]=目标[“数量”]+1;
}
打印(cartList[1].toString());
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
风格:Theme.of(context).textTheme.headline4,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}
您可以复制粘贴运行下面的完整代码您可以使用
firstWhere
并检查id
代码片段
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),
),
);
}
}
导入“包装:颤振/材料.省道”;
void main(){
runApp(MyApp());
}
类MyApp扩展了无状态小部件{
@凌驾
小部件构建(构建上下文){
返回材料PP(
标题:“颤振演示”,
主题:主题数据(
主样本:颜色。蓝色,
视觉密度:视觉密度。自适应平台密度,
),
主页:MyHomePage(标题:“颤振演示主页”),
);
}
}
类MyHomePage扩展StatefulWidget{
MyHomePage({Key,this.title}):超级(Key:Key);
最后的字符串标题;
@凌驾
_MyHomePageState createState()=>\u MyHomePageState();
}
类_MyHomePageState扩展状态{
int _计数器=0;
列表cartList=[
{“id”:“asdasy21dh”,“价格”:23,“数量”:2},
{“id”:“asd21aa”,“价格”:43,“数量”:1},
];
void _incrementCounter(){
var target=cartList.firstWhere((项)=>item[“id”]==“asd21aa”);
如果(目标!=null){
目标[“数量”]=目标[“数量”]+1;
}
打印(cartList[1].toString());
设置状态(){
_计数器++;
});
}
@凌驾
小部件构建(构建上下文){
返回脚手架(
appBar:appBar(
标题:文本(widget.title),
),
正文:中(
子:列(
mainAxisAlignment:mainAxisAlignment.center,
儿童:[
正文(
“您已经按了这么多次按钮:”,
),
正文(
“$”计数器“,
风格:Theme.of(context).textTheme.headline4,
),
],
),
),
浮动操作按钮:浮动操作按钮(
按下时:\ u递增计数器,
工具提示:“增量”,
子:图标(Icons.add),
),
);
}
}