无法将参数类型“ dynamic”分配给参数类型“ String”

时间:2020-09-15 13:22:37

标签: flutter

我试图在我的一个应用程序屏幕中完成一个可滚动的水平列表,但出现抖动,但出现以下错误:

不能将参数类型“ dynamic”分配给参数类型“ String”。

代码如下:

类别CategoryListState扩展了状态{

  int selectedIndex = 0;
  List categories = ['Checkboxes', 'DropDown', 'SwipeCards', 'SwipeCards', 'SwipeCards', 'SwipeCards',];

  @override
  Widget build(BuildContext context) {
    return Container(
      margin: EdgeInsets.symmetric(vertical: 20.0/2),
      height: 30.0,
      child: ListView.builder(
        scrollDirection: Axis.horizontal,
        itemCount: categories.length,
        itemBuilder: (context, index) => Container (
          alignment: Alignment.center,
          margin: EdgeInsets.symmetric(horizontal: 20.0),
          padding: EdgeInsets.symmetric(horizontal: 20.0),
          decoration: BoxDecoration(
            color: Colors.white.withOpacity(0.4),
            borderRadius: BorderRadius.circular(6),
          ),
          child: Text(
            categories[index],
          ),

        ),

      ),
    );
  }
}

我很感谢。

2 个答案:

答案 0 :(得分:0)

更改为List<String> categoriescategories[index].toString()

答案 1 :(得分:0)

尝试这样做

child: Text(
            categories[index].toString(),
          ),

List<String> categories = ['Checkboxes', 'DropDown', 'SwipeCards', 'SwipeCards', 'SwipeCards', 'SwipeCards',];