Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The body might complete normally, causing 'null' to be returned, but the return type, 'Widget' #83

Open
Ange0 opened this issue May 25, 2022 · 1 comment

Comments

@Ange0
Copy link

Ange0 commented May 25, 2022

Hello guys,
I had trouble implementing this code when I copied and pasted it into project, so I added some code snippets to make it work for me.
// befor

RatingBar.builder(
initialRating: 3,
itemCount: 5,
itemBuilder: (context, index) {
switch (index) {
case 0:
return Icon(
Icons.sentiment_very_dissatisfied,
color: Colors.red,
);
case 1:
return Icon(
Icons.sentiment_dissatisfied,
color: Colors.redAccent,
);
case 2:
return Icon(
Icons.sentiment_neutral,
color: Colors.amber,
);
case 3:
return Icon(
Icons.sentiment_satisfied,
color: Colors.lightGreen,
);
case 4:
return Icon(
Icons.sentiment_very_satisfied,
color: Colors.green,
);
}
},
onRatingUpdate: (rating) {
print(rating);
},
;

// after

RatingBar.builder(
initialRating: 3,
itemCount: 5,
itemBuilder: (context, index) {
switch (index) {
case 0:
return Icon(
Icons.sentiment_very_dissatisfied,
color: Colors.red,
);
case 1:
return Icon(
Icons.sentiment_dissatisfied,
color: Colors.redAccent,
);
case 2:
return Icon(
Icons.sentiment_neutral,
color: Colors.amber,
);
case 3:
return Icon(
Icons.sentiment_satisfied,
color: Colors.lightGreen,
);
case 4:
return Icon(
Icons.sentiment_very_satisfied,
color: Colors.green,
);
}
return Container();
},
onRatingUpdate: (rating) {
print(rating);
},
)

@ghassenbenzahra123
Copy link

ghassenbenzahra123 commented Sep 23, 2023

@Ange0 the builder function requires a widget to return. In this case, you are returning the widget but it is in a switch statement. This means that if the switch statement didn't satisfy, your builder will return null which is not acceptable.

You can fix it by just adding a default case for the switch statement like this:
default: return const SizedBox();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants