Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'_Type' is not a subtype of type 'Widget'

Tags:

flutter

widget

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,
                                ),
                              ]));
                        }),
          )
like image 900
Samet ÖZTOPRAK Avatar asked Mar 01 '19 11:03

Samet ÖZTOPRAK


Video Answer


4 Answers

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();
like image 24
Paresh Mangukiya Avatar answered Oct 05 '22 06:10

Paresh Mangukiya


Your mistake is that you returned the type instead of the instance of a given type:

return Foo;

vs

return Foo();
like image 58
Rémi Rousselet Avatar answered Oct 08 '22 04:10

Rémi Rousselet


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.

like image 3
Dan Lee Avatar answered Oct 08 '22 04:10

Dan Lee


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.

like image 1
Dev Shah Avatar answered Oct 08 '22 03:10

Dev Shah