List<dynamic> is not a subtype of type List<Widget>?

I am trying CarouselSlider.I want to make the upside Carousel Slider,bottom side TextFormField. I am holding a list of image paths in selected and send to the screen like this:

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

After that I am taking arguments in other screen like this:

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

Later

I am putting this list into 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,
    ))]

    ));
  }

I am getting List is not a subtype of type List? error and I couldn’t find the solution for this error can anyone help me to find a solution?

>Solution :

Seems like args['image'] can be null that’s why you get this error because items should be of type List<Widget>. Try creating the variable this way.

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

However, you may want to check first if images.isNotEmpty beforing mapping it.

Leave a Reply