在 flutter 中获取地图索引的问题
Problem with getting access to index of a map in flutter
我有一个地图 ,并希望通过 ListView.Builder 在卡片中显示它的组件,但问题是如何访问此地图的索引..
当 运行 应用程序时,它 return 在卡中为“空”!!
对于类似的问题,我在 Whosebug 中看到的许多解决方案都尝试过,但没有结果。
在这里我找到了我的地图:
var _item;
List listCount = [];
Map<String, dynamic> records = {};
String name;
在这里我给 var _item 赋值:
MyCard(
colour: Colors.lightBlueAccent,
maker: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
StreamBuilder<int>(
stream: _stopWatchTimer2.rawTime,
initialData: 0,
builder: (context, snap) {
final value = snap.data;
final displayTime = StopWatchTimer.getDisplayTime(
value,
hours: _isHours2);
_item = displayTime;
return Padding(
padding: EdgeInsets.all(5.0),
child: Text(displayTime,
style: TextStyle(
fontSize: 30.0, color: Colors.white)),
);
},
),
],
),
),
在“保存”按钮中,我给出了地图的第一个参数并在名称变量中分配:
createAlertDialog(buildContext, context) {
TextEditingController controller;
return showDialog(
context: context,
// barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Text(
'Type record name',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18.0),
),
content: TextField(
controller: controller,
onChanged: (value) {
name = value;
}),
actions: [
MaterialButton(
elevation: 5.0,
child: Text('Save'),
onPressed: () {
listCount.add(_item);
print(_item);
records[name] = _item;
print(records);
Navigator.pop(context);
},
),
MaterialButton(
elevation: 5.0,
child: Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
),
],
);
},
);
}
终于在这里我试图通过 listview.builder 在卡片中展示它:
Container(
color: Colors.white,
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: records.length,
itemBuilder: (context, index) {
return MyCard(
colour: Colors.cyanAccent,
maker: Container(
width: 250.0,
height: 75.0,
child: Text(
'${records[index]}',
style: TextStyle(fontSize: 25.0),
textAlign: TextAlign.center,
),
),
);
},
),
),
我认为它(索引)的 itemBuilder 的第二个参数有问题,因为我随机用另一个关键字替换了这个关键字,并且当 运行 我的应用程序“Null”时得到相同的结果。
This screenShot explain the problem:
您可以这样获取地图条目:
final recordsEntries = records.entries.toList()
然后,您有一个 MapEntry 列表,并且能够访问与集合中的每个项目关联的键和值。
recordsEntries[index].key
recordsEntries[index].value
我有一个地图
当 运行 应用程序时,它 return 在卡中为“空”!!
对于类似的问题,我在 Whosebug 中看到的许多解决方案都尝试过,但没有结果。
在这里我找到了我的地图:
var _item;
List listCount = [];
Map<String, dynamic> records = {};
String name;
在这里我给 var _item 赋值:
MyCard(
colour: Colors.lightBlueAccent,
maker: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
StreamBuilder<int>(
stream: _stopWatchTimer2.rawTime,
initialData: 0,
builder: (context, snap) {
final value = snap.data;
final displayTime = StopWatchTimer.getDisplayTime(
value,
hours: _isHours2);
_item = displayTime;
return Padding(
padding: EdgeInsets.all(5.0),
child: Text(displayTime,
style: TextStyle(
fontSize: 30.0, color: Colors.white)),
);
},
),
],
),
),
在“保存”按钮中,我给出了地图的第一个参数并在名称变量中分配:
createAlertDialog(buildContext, context) {
TextEditingController controller;
return showDialog(
context: context,
// barrierDismissible: false,
builder: (context) {
return AlertDialog(
title: Text(
'Type record name',
textAlign: TextAlign.center,
style: TextStyle(fontSize: 18.0),
),
content: TextField(
controller: controller,
onChanged: (value) {
name = value;
}),
actions: [
MaterialButton(
elevation: 5.0,
child: Text('Save'),
onPressed: () {
listCount.add(_item);
print(_item);
records[name] = _item;
print(records);
Navigator.pop(context);
},
),
MaterialButton(
elevation: 5.0,
child: Text('Cancel'),
onPressed: () {
Navigator.pop(context);
},
),
],
);
},
);
}
终于在这里我试图通过 listview.builder 在卡片中展示它:
Container(
color: Colors.white,
child: ListView.builder(
scrollDirection: Axis.vertical,
shrinkWrap: true,
itemCount: records.length,
itemBuilder: (context, index) {
return MyCard(
colour: Colors.cyanAccent,
maker: Container(
width: 250.0,
height: 75.0,
child: Text(
'${records[index]}',
style: TextStyle(fontSize: 25.0),
textAlign: TextAlign.center,
),
),
);
},
),
),
我认为它(索引)的 itemBuilder 的第二个参数有问题,因为我随机用另一个关键字替换了这个关键字,并且当 运行 我的应用程序“Null”时得到相同的结果。
This screenShot explain the problem:
您可以这样获取地图条目:
final recordsEntries = records.entries.toList()
然后,您有一个 MapEntry 列表,并且能够访问与集合中的每个项目关联的键和值。
recordsEntries[index].key
recordsEntries[index].value