Hello Guys How are you all? Hope You all are fine. When I was trying to run my flutter app and suddenly I get the following error in my stack track. the element type ‘Listwidget’ can’t be assigned to the list type ‘Widget’ in a flutter. So today Here I come with all possible solutions for this error.
We are providing you all possible solutions to solve this error. let’s start this article without wasting your time.
How the element type ‘Listwidget’ can’t be assigned to the list type ‘Widget’ Error Occurs ?
I have Users List. I am making userTile for number of users found in for loop. Here is my code.
List<Widget> getUserList() {
List<Widget> data = [];
for (var i = 0; i < 10; i++) {
data.add(new ListItem('user_id' + $i));
}
return data;
}
here I am accessing getUserList
children: <Widget>[getUserList()],
But it is showing compile time error.
The element type 'List<widget>' can't be assigned to the list type 'Widget'
How to Solve the element type ‘Listwidget’ can’t be assigned to the list type ‘Widget’ Error?
- How to Solve the element type 'Listwidget' can't be assigned to the list type 'Widget' Error?
to Solve the element type 'Listwidget' can't be assigned to the list type 'Widget' Error Just wrapping a list as list children: [getUserList()], This should be like this. children: getUserList(),
- the element type 'Listwidget' can't be assigned to the list type 'Widget'
to Solve the element type 'Listwidget' can't be assigned to the list type 'Widget' Error Just wrapping a list as list children: [getUserList()], This should be like this. children: getUserList(),
Solution 1
Just wrapping a list as list
children: <Widget>[getUserList()],
This should be like this.
children: getUserList(),
Solution 2
Just use spread operator:
children: <Widget>[...getUserList()]
Summery
So, It’s All About This Error. I hope this tutorial helps you to Solve your error. Please Comment Below if You stucks anywhere with my code. And please comment below on which solution worked for you. Thank You.
Also Check Out Below Tutorials
- Failed to launch emulator: Error: Emulator didn’t connect within 60 seconds
- Finished with error: Gradle task assembleDebug failed with exit code 1
- error: ‘Flutter/Flutter.h’ file not found when flutter run on iOS
- Cannot run with sound null safety because dependencies don’t support null safety
- Null check operator used on a null value
Thanks this post saved me, both solution worked for me but i just picked the second one, Thanks again