代码之家  ›  专栏  ›  技术社区  ›  Jessica

错误:无法推断类型参数“t”。使用时Map.fromIterable

  •  0
  • Jessica  · 技术社区  · 5 年前

    我想把 List 到a Map<int, Widget> CupertinoSlidingSegmentedControl . 我试着用 Map.fromIterable

    我的代码是:

    final List<String> tabValues = ['first', 'second', 'third'];
    
    CupertinoSlidingSegmentedControl(
              groupValue: selectedTab,
              onValueChanged: _onTabChange,
              children: Map.fromIterable(
                tabValues,
                value: (element) => _tabContent(element),
              ),
            )
    
    Widget _tabContent(String text) {
      return Text(text);
    }
    

    无法推断类型参数“t”。

    1 回复  |  直到 5 年前
        1
  •  0
  •   smac89    5 年前

    这个 Map.fromIterable 函数要求两者都是 key value 映射函数。

    Map<T, T> 其中键和值函数被identity函数替换。


    children: tabValues.asMap().map((index, tabValue) => MapEntry(index, _tabContent(tabValue)))
    

    这个 钥匙 tabValues.asMap() ,所以现在我们只需要打电话 .map tabValue