如何解决我正在尝试从Dart映射中获取键和相应的值,但是无论尝试如何,我都失败了
这里是数据;这是一个列表,其中一项(信贷供应商)是k,v对的映射。我想将每个键和每个值放在自己的Text小部件中。代码如下。
#input: 5 2 8 4 5 0
START INP
BRZ OUTPUT
STA MAX
LOOP INP
BRZ OUTPUT
STA NUM
LDA MAX
SUB NUM
BRP LOOP ; input was not greater
LDA NUM
STA MAX ; input was greater
BRA LOOP
OUTPUT LDA MAX
OUT
HLT
NUM DAT
MAX DAT 0
<script src="https://cdn.jsdelivr.net/gh/trincot/lmc@v0.7/lmc.js"></script>
因此,我想在不同页面上的另一个类中调用creditVendors值。下面的代码说明了我要实现的目标。
List<Blog> blogs = [
Blog(
title:
"She changed religion to marry the man of her dreams- Hamidah and Efraim’s classy Kwanjula ",description:
"Right after their arrival,Efraim and a select few of his entourage went right into "
"Hamidah’s parents’ house for the basic session of kwanula: to be accepted and born into this home. \n",author: "James Arthur",date: "09 Jan 20",views: 3899,comments: 23,imagePath: "assets/images/intro.jpg",creditVendors: [
{'Decorator': 'Parties & Events'},{'Photography': 'Paramount Images'},{'Food': 'Spectrum Restaurant'},{'Juice': 'Dalausi Juice'},{'Bridal': 'Sheena Collections'},],),Blog(),];
解决方法
您可以通过其值或键在地图上进行迭代。因为您的creditVendors
本身就是一个数组,所以这不是必需的。
然后,您可以使用ListView
小部件从地图的值创建自己选择的小部件。
示例代码
List<dynamic> creditVendors = [
{'Decorator': 'Parties & Events'},{'Photography': 'Paramount Images'},{'Food': 'Spectrum Restaurant'},{'Juice': 'Dalausi Juice'},{'Bridal': 'Sheena Collections'},];
ListView.builder(
itemCount: creditVendors.length,itemBuilder: (ctx,int index) {
return ListTile(
*emphasized text* title: Text(
'${creditVendors[index].keys.first} - ${creditVendors[index].values.first}'));
}),
- 您可以使用
mapName.keys
方法从地图上获取所有键 - 您可以使用
mapName.values
方法从地图中获取所有值
更新:
由于要遍历两个列表,因此必须使用嵌套的ListView
->一个是blogs数组,并且要在其中遍历该Blog的creditVendors
列表。
请查看演示代码:
ListView.builder(
itemCount: widget.blog.length,shrinkWrap: true,int _blogIndex) {
Blog_blog = blogs[_blogIndex];
return ListView.builder(
itemCount: _blog['creditVendors'].length,int index) {
return ListTile(
title: Text(
'${_blog['creditVendors'][index].keys.first} - ${_blog['creditVendors'][index].values.first}'));
});
}))
,
我希望您知道博客列表中只有一个Blog
类型的元素
所以我只做一个元素。如果您只需要迭代休息,一切都会一样。
您应该执行以下操作:
Blog a = blog[0]; //this will put the first element in a
然后:
将变量定义为映射,因为creditVendors
中的内容属于maps
类型
并且creditVendors
还是list
数据类型。
// this is just to collect the data inside creditVendors.
Map decorator;
Map photography;
Map food;
Map juice;
Map bridal;
decorator = a.creditVendors[0];
photography = a.creditVendors[1];
food = a.creditVendors[2];
juice = a.creditVendors[3];
bridal = a.creditVendors[4];
现在:
您可以按以下方式访问每个地图数据:
Text(decorator['Decorator'],)
Text(photography['Photography'],)
Text(food['Food'],)
Text(juice['Juice'],)
Text(bridal['Bridal'],)
PS。 CreditVendor是地图列表
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。