Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Flutter getting the following error The return type 'List<Widget>' isn't a 'Widget'

I’m getting the following error The return type 'List<Widget>' isn't a 'Widget', as required by the closure's context. It’s happing on _buildUserStatsSectionItems

Here is my main widget

Widget build(BuildContext context) {
    return StreamBuilder<DocumentSnapshot<Object?>>(
      stream: userDoc,
      builder: (
        BuildContext context,
        AsyncSnapshot<DocumentSnapshot<Object?>> snapshot,
      ) {
        if (snapshot.hasError) {
          return const Text('Something went wrong');
        }

        if (snapshot.connectionState == ConnectionState.waiting) {
          return Row(
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: const [
              Text(
                "Loading",
                style: AppStyles.dashboardStatsTextStyle,
              ),
            ],
          );
        }
        return _buildUserStatsSectionItems(snapshot.data);
      },
    );
  }

Here is the _buildUserStatsSectionItems

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

List<Widget> _buildUserStatsSectionItems(data) {
  return <Widget>[
    Column(
      children: [
        Text(
          data['totalTrainingSessions'].toString(),
          style: AppStyles.dashboardStatsTextStyle,
        ),
        const Text(
          'sessions',
          textAlign: TextAlign.center,
          style: AppStyles.dashboardStatsSubTextStyle,
        )
      ],
    ),
    Column(
      children: [
        Text(
          data['totalTrainingDays'].toString(),
          style: AppStyles.dashboardStatsTextStyle,
        ),
        const Text(
          'days',
          textAlign: TextAlign.center,
          style: AppStyles.dashboardStatsSubTextStyle,
        )
      ],
    ),
    Column(
      children: [
        Text(
          data['totalTrainingTime'].toString(),
          style: AppStyles.dashboardStatsTextStyle,
        ),
        const Text(
          'hours',
          textAlign: TextAlign.center,
          style: AppStyles.dashboardStatsSubTextStyle,
        )
      ],
    ),
    Column(
      children: [
        Text(
          data['dogsCount'].toString(),
          style: AppStyles.dashboardStatsTextStyle,
        ),
        const Text(
          'dogs',
          textAlign: TextAlign.center,
          style: AppStyles.dashboardStatsSubTextStyle,
        )
      ],
    ),
  ];
}

>Solution :

The return type of _buildUserStatsSectionItems is List<Widget>. Based on the StreamBuilder documentation, the AsyncWidgetBuilder should return Widget – this is where the error comes from.

To resolve your issue, you should wrap your widgets inside _buildUserStatsSectionItems with Wrap or Column:

Widget _buildUserStatsSectionItems(data) {
  return Column(
    children: [...], // Add your current Widget list here
  );
}
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading