如何遍历地图列表,以便一路获得最终价值?

时间:2019-10-25 11:15:58

标签: flutter dart flutter-layout

所以我有一个地图列表(当前有一个值)...列表中的每个值都是一个以字符串为键的地图和一个(有字符串键和字符串值的)地图列表值...。我正在尝试获取键的每个值,即imgpath和error

但是,我得到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,
      ),
    );
  }

1 个答案:

答案 0 :(得分:0)

如果我理解正确,则无需使用.map(),可以这样做:

imgpath = errorInfo[index].values.first['imgpath'];
error = errorInfo[index].values.first['error'];