【发布时间】:2019-10-25 11:15:58
【问题描述】:
所以我有一个映射列表(当前有一个值)...列表中的每个值都是一个映射,其中一个字符串作为键和一个列表(映射(带有字符串键和字符串值))作为值....我正在尝试获取键的每个值,即 imgpath 和错误
但是,我得到 null .....我怎么能不这样??..screenshot here
class _ErrorsCapturedState extends State<ErrorsCaptured> {
Widget _createError(BuildContext context, index) {
String imgpath;
String error;
List<Map<String, Map>> errorInfo = [
{
'Transmitter': {
'imgpath':null,
// 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTh-U8FgtxrN0Zcgs4ETPKGtxBUbqmncDQlh5IYcGT5bxsDi92a',
'error': 'Transmitter error detected'
},
},
{
'Antenna': {
'imgpath':null,
// 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTh-U8FgtxrN0Zcgs4ETPKGtxBUbqmncDQlh5IYcGT5bxsDi92a',
'error': 'Antenna error detected'
},
},
{
'Uninterrupted': {
'imgpath':null,
// 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTh-U8FgtxrN0Zcgs4ETPKGtxBUbqmncDQlh5IYcGT5bxsDi92a',
'error': 'Obvious error detected'
},
},
{
'Satellite receiver': {
'imgpath':null,
// 'https://encrypted-tbn0.gstatic.com/images?q=tbn%3AANd9GcTh-U8FgtxrN0Zcgs4ETPKGtxBUbqmncDQlh5IYcGT5bxsDi92a',
'error': 'another error detected'
},
},
// 'Antenna System',
// 'U.P.S',
// 'Satellite Receiver',
// 'Mains Power',
]
errorInfo.map(
(value) {
print(errorInfo);
imgpath = index[value]['imgpath'];
error = index[value]['error'];
},
);
var imgcreated;
if (imgpath != null) {
imgcreated = Image.network(
imgpath,
fit: BoxFit.fill,
);
} else {
imgcreated = Material(
borderRadius: BorderRadius.only(
topLeft: Radius.circular(10),
topRight: Radius.circular(10),
),
child: Image.asset(
'assets/flat.jpg',
fit: BoxFit.fill,
),
);
}
var text;
if (error != null) {
text = Text('$error');
} else {
text = Text('No errors found!');
}
var sizedBox = SizedBox(
height: 10,
);
return Padding(
padding: const EdgeInsets.all(10.0),
child: Material(
elevation: 20.0,
borderRadius: BorderRadius.circular(10.0),
child: Container(
padding: EdgeInsets.all(8),
width: 300,
child: CustomScrollView(
slivers: <Widget>[
SliverAppBar(
centerTitle: false,
expandedHeight: 200,
pinned: true,
flexibleSpace:
FlexibleSpaceBar(title: text, background: imgcreated),
),
SliverFillRemaining(
child: Wrap(
children: <Widget>[
Placeholder(
color: Colors.red,
)
],
),
)
],
),
),
shadowColor: Theme.of(context).primaryColor,
),
);
}
【问题讨论】:
-
尚不清楚
errorInfo是否会总是包含 1 个项目。如果不是,那么您的其余代码就没有任何意义。请告诉我是否是这种情况。 -
检查已编辑的问题....我在图像路径上使用了 null 以使其更易于阅读
-
我不太明白你用
errorInfo.map()实现了什么,因为你不断地覆盖这些值。
标签: flutter dart flutter-layout