Reputation: 359
I am trying to set the initial value of a boolean from a value passed into my widget. My attempt right now looks like:
const CustomLikeButton({
Key? key,
required this.liked,
}) : super(key: key);
final bool liked;
@override
State<CustomLikeButton> createState() => _CustomLikeButtonState();
}
class _CustomLikeButtonState extends State<CustomLikeButton> {
bool _liked;
@override
void initState() {
bool _liked = widget.liked;
super.initState();
}
but it is giving the error:
Non-nullable instance field '_liked' must be initialized.
Try adding an initializer expression, or a generative constructor that initializes it, or mark it 'late'.
Anyone know what is going wrong here?
Thank you!
Upvotes: 0
Views: 733
Reputation: 4404
its null safety
you define bool _liked;
but without any value.
you can try
late bool _liked;
or
bool _liked = false; // or true
or
bool? _liked;
then set the value you get from the constructor
_liked = widget.liked
Upvotes: 2