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
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,
)
],
),
];
}
CodePudding user response:
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
);
}
CodePudding user response:
StreamBuilder
apparently takes only one widget as return value from the builder.
Just wrap your function in a Column / Row
like so:
return Column(children:_buildUserStatsSectionItems(snapshot.data));
or make your function return only one Widget.