1

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,
      ),
    );
  }
}

1
  • Try children: widget.value as List<Widget> Commented Oct 5, 2021 at 22:04

1 Answer 1

1

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?

Sign up to request clarification or add additional context in comments.

13 Comments

That didn't seem to fix the problem, I'm still getting the same error
what are you passing as value, can you include that. also try flutter clean and run again.
does it solve or what errors you are currently getting?
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);
ok you are just passing list of String at NextPage, am i right?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.