chichi
chichi

Reputation: 3292

Flutter: type 'List<Widget>' is not a subtype of type 'Widget'

class UserList extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    final List _user = ['aa', 'bb', 'cc'];
    return Container(
      height: 150,
      child: ListView(
        shrinkWrap: true,
        scrollDirection: Axis.horizontal,
        children: [
            return (_user.length != 0)
                ? List.generate(_user.length, (index) {
                    return GestureDetector(
                      onTap: () {
                        print('tapped!');
                      },
                      child: Container(
                        decoration: new BoxDecoration(
                          color: Colors.blueAccent,
                          borderRadius: new BorderRadius.all(
                            const Radius.circular(40.0),
                          ),
                        ),
                        width: 150,
                      ),
                    );
                  })
                : GestureDetector(
                    onTap: () {
                      print('hehehe');
                    },
                    child: Container(
                      decoration: new BoxDecoration(
                        color: Colors.blueAccent,
                        borderRadius: new BorderRadius.all(
                          const Radius.circular(40.0),
                        ),
                      ),
                      width: 150,
                      child: Center(
                        child: Text('Whhaaaat'),
                      ),
                    ),
                  );
        ],
      ),
    );
  }
}

I am trying to create List of cards according to the number of users. Tried to do ...List.generate as well. If I try with ...List.generate then I receive 'Expect to find :'. I also have to add ';' which causes 'Expect to find;'.

However, I am getting type 'List<Widget>' is not a subtype of type 'Widget' error. What can I do to convert the List<Widget> to Widget type in this case?

Upvotes: 2

Views: 6468

Answers (1)

Bruno J.
Bruno J.

Reputation: 252

Your issue is that you are returning an object to 'Children' which is not a widget, in this case, a 'List'.

If you want to display dynamic content, make sure to use 'Listview.builder()'

Upvotes: 1

Related Questions