I took the red screen '_Type' is not a subtype of type 'Widget' exception in NoContentPage I wrote down the full component code and called place into StatefulWidget. What is wrong in this code. I think it is a very common exception in the flutter.
class NoContentPage extends StatelessWidget {
@override
Widget build(BuildContext context) {
return (new Container(
width: 320.0,
child: new Column(
children: <Widget>[
Center(
child: Text(AppLocalizations.of(context).messageNoContent,
style: TextStyle(
color: Colors.black,
fontSize: 20.0,
fontWeight: FontWeight.w300,
letterSpacing: 0.3,
)))
],
),
));
}
}
called
body: Container(
child: videos == null
? Center(child: CircularProgressIndicator())
: videos.length == 0
? NoContentPage
: ListView.builder(
itemCount: videos.length,
itemBuilder: (context, index) {
return GestureDetector(
onTap: () {
playYoutubeVideo(videos[index][Video.videoUrl]);
},
child: Column(children: [
new YoutubeCard(
video: videos[index],
),
new Divider(
height: 0.0,
color: Colors.grey,
),
]));
}),
)
NoContentPage
should be an Instance
of a widget.
so instead of
videos.length == 0 ? NoContentPage : ListView.builder()
it should be
videos.length == 0 ? NoContentPage() : ListView.builder()
For ease of understanding Example:
wrong:
return Foo;
right:
return Foo();
Your mistake is that you returned the type instead of the instance of a given type:
return Foo;
vs
return Foo();
I have faced a very similar problem with error info '' when having following code:
List<Widget> items = ['1', '2', '3', '4']
.map(
(f) {
return Text(f);
},
)
.toList();
items.last = Stack(
children: <Widget>[items.last],
);
change this code to following works:
List<Widget> items = ['1', '2', '3', '4']
.map(
(f) {
return Text(f);
},
)
.cast<Widget>()
.toList();
items.last = Stack(
children: <Widget>[items.last],
);
I think maybe when doing a map, the result type from map function is changed, so the internal dart implemention of list assert fail. Force the cast type works.
Also, this error is very common when you are moving across screens and using network data to be displayed in your apps, using ternary operator for data and progress indicator then the indicator must be called as a Widget LinearProgressIndicator()
and not LinearProgressIndicator
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With