0

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!

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.