【问题标题】:How can i iterate through a list of maps so that i get the end value all the way?我如何遍历地图列表以便一路获得最终值?
【发布时间】: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


【解决方案1】:

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

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-08-19
    • 1970-01-01
    • 2022-01-13
    • 2019-12-02
    • 1970-01-01
    • 2018-05-14
    相关资源
    最近更新 更多