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?
Author by
StrWrs_Nerd
Updated on January 01, 2023Comments
-
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 over 2 yearsTry
children: widget.value as List<Widget>
-
-
StrWrs_Nerd over 2 yearsThat didn't seem to fix the problem, I'm still getting the same error
-
Yeasin Sheikh over 2 yearswhat are you passing as value, can you include that. also try
flutter clean
and run again. -
Yeasin Sheikh over 2 yearsdoes it solve or what errors you are currently getting?
-
StrWrs_Nerd over 2 yearsWhen 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 over 2 yearsok you are just passing list of String at
NextPage
, am i right? -
StrWrs_Nerd over 2 yearsYes I am with this:
new NextPage(value: _remindersList),
-
Yeasin Sheikh over 2 yearsin that case we need to use those string and convert them as Widget for ListView. let me update and share a demo.
-
StrWrs_Nerd over 2 yearsSorry I'm quite new to asking questions on stackoverflow, how do I do that?
-
Yeasin Sheikh over 2 yearsI've updated the answer, does it solve the issue now?
-
StrWrs_Nerd over 2 yearsAm I replacing NextPage with that or _NextPageState?
-
Yeasin Sheikh over 2 yearsJust replace
children: widget.value,
withwidget.value.map((e) => Text(e)).toList(),.
hope it will do the work. -
StrWrs_Nerd over 2 yearsIt worked! Thanks for your help
-
Yeasin Sheikh over 2 yearsGlad to help you out.