首页 > 解决方案 > 列表不是 List 类型的子类型?

问题描述

我正在尝试 CarouselSlider。我想制作顶部的 Carousel Slider,底部的 TextFormField。我持有一个图像路径列表selected并发送到屏幕,如下所示:

ElevatedButton(
                          onPressed: () {
                            Navigator.of(context).pushNamed(
                              OcrScreen.routeName,
                              arguments: {'image': selected},
                            );
                          },

之后,我在其他屏幕上进行辩论,如下所示:

final args = ModalRoute.of(context)!.settings.arguments as Map;
    final images=args['image'];

之后

我将此列表放入 CarouselSlider:

return Scaffold(extendBodyBehindAppBar: false,
      appBar: AppBar(
        title: const Text('OCR'),
        elevation: 0,
        backgroundColor: Colors.transparent,
      ),
    body:Column(children:[Container(
    child: CarouselSlider(
    options: CarouselOptions(),
      items: images
          .map((image) => Container(
        child: Center(
            child:
            Image.file(File(image),fit: BoxFit.cover, width: 1000)),
      ))
          .toList(),
    )),TextFormField(
        keyboardType: TextInputType.multiline,

        controller: _titleController,
    autofocus: false,
    style: TextStyle(fontSize: 22.0, color: Colors.black),
        maxLines: 20,
    decoration: InputDecoration(
    filled: true,
      isDense: true,


      contentPadding: new EdgeInsets.symmetric(vertical: 10.0, horizontal: 10.0),

      fillColor: Colors.white,
    ))]

    ));
  }

我得到的 List 不是 List 类型的子类型?错误,我找不到此错误的解决方案,谁能帮我找到解决方案?

标签: flutterflutter-layout

解决方案


似乎这就是您收到此错误的原因,因为args['image']应该是 type 。尝试以这种方式创建变量。nullitemsList<Widget>

final List<String> images = args['image'] ?? [];

但是,您可能需要在images.isNotEmpty映射之前先检查一下。


推荐阅读