【问题标题】:Convert List of Maps to Stepper in flutter?在颤动中将地图列表转换为步进器?
【发布时间】:2019-04-26 14:01:06
【问题描述】:

我正在使用颤振创建一个简单的待办事项列表应用程序。我想从地图列表中创建一个步进器小部件,如下所示。

[
      { 
        'task': 'Cook for 5 min',
        'content': '30',
      },
      {
        'task': 'Stir gently',
        'content': '20',
      },
]

目前我正在我的构建器函数中执行此操作:

List<Step> todoSteps = args['todos'].map<Step>((todo) {
      return Step(
        title: Text(todo['task']),
        content: Text(todo['content']),
        isActive: _currentStep >= i, // this is the issue
      );
    }).toList();

然后我继续使用 todoSteps 设置 Stepper 小部件的 steps 参数。

除了 isActive 参数我必须检查 _currentStep 是否小于索引 (i) 之外,这大部分都可以正常工作。但问题是当我似乎无法获得函数中的索引时。

我尝试在列表上使用 asMap() 函数,然后读取索引,但总是导致错误。

【问题讨论】:

    标签: flutter dart


    【解决方案1】:

    .asMap().entries 应该提供您所需要的:

    List<Step> todoSteps = args['todos'].asMap().entries.map<Step>((e) {
        var i = e.key;
        var todo = e.value;
        return Step(
          title: Text(todo['task']),
          content: Text(todo['content']),
          isActive: _currentStep >= i, // this is the issue
        );
      }).toList();
    

    【讨论】:

      猜你喜欢
      • 2019-02-06
      • 2021-12-22
      • 2020-10-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-01
      • 1970-01-01
      相关资源
      最近更新 更多