type 'List<dynamic>' is not a subtype of type 'List<Widget>' How do I use a List<Dynamic> in a listview widget?

125

According to the discussion, value is List<String> in that case, to pass it on ListView we need to use these data and convert them into widget.

Here we are using value and making Text widget with it


import 'package:flutter/material.dart';

class NextPage extends StatefulWidget {
  final List<String> value;

  NextPage({Key key, this.value}) : super(key: key);

  @override
  _NextPageState createState() => new _NextPageState();
}

class _NextPageState extends State<NextPage> {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Reminders"),
      ),
      body: ListView(
       children: widget.value.map((e) => Text(e)).toList(),
      ),
    );
  }
}

Does it solve the issue?

Share:
125
StrWrs_Nerd
Author by

StrWrs_Nerd

Updated on January 01, 2023

Comments

  • StrWrs_Nerd
    StrWrs_Nerd over 1 year

    I am trying to use a list in a Listview widget in flutter but I keep getting an error saying that I need a List[Widget]. All the answers online make use of maps and I am still a beginner to Flutter. Can anyone show me how to use the map function for this or a way to convert List [Dynamic] to List [Widget]?

    Here is my code:

    import 'package:flutter/material.dart';
    
    class NextPage extends StatefulWidget {
      final List value;
    
      NextPage({Key key, this.value}) : super(key: key);
    
      @override
      _NextPageState createState() => new _NextPageState();
    }
    
    class _NextPageState extends State<NextPage> {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: new AppBar(
            title: new Text("Reminders"),
          ),
          body: ListView(
            children: widget.value,
          ),
        );
      }
    }
    
    
    • Anas Nadeem
      Anas Nadeem over 2 years
      Try children: widget.value as List<Widget>
  • StrWrs_Nerd
    StrWrs_Nerd over 2 years
    That didn't seem to fix the problem, I'm still getting the same error
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    what are you passing as value, can you include that. also try flutter clean and run again.
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    does it solve or what errors you are currently getting?
  • StrWrs_Nerd
    StrWrs_Nerd over 2 years
    When I use the type cast method the error says type 'List<dynamic>' is not a subtype of type 'List<Widget>' in type cast I am passing in a list of strings that i recieve from an await function like this: _remindersList = await _analyze(Words);
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    ok you are just passing list of String at NextPage, am i right?
  • StrWrs_Nerd
    StrWrs_Nerd over 2 years
    Yes I am with this: new NextPage(value: _remindersList),
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    in that case we need to use those string and convert them as Widget for ListView. let me update and share a demo.
  • StrWrs_Nerd
    StrWrs_Nerd over 2 years
    Sorry I'm quite new to asking questions on stackoverflow, how do I do that?
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    I've updated the answer, does it solve the issue now?
  • StrWrs_Nerd
    StrWrs_Nerd over 2 years
    Am I replacing NextPage with that or _NextPageState?
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    Just replace children: widget.value, with widget.value.map((e) => Text(e)).toList(),. hope it will do the work.
  • StrWrs_Nerd
    StrWrs_Nerd over 2 years
    It worked! Thanks for your help
  • Yeasin Sheikh
    Yeasin Sheikh over 2 years
    Glad to help you out.