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

How can I make a variable in the constructor make it optional (flutter)?

please help me solve the following problem.
This part of my code:

class MeditationCard extends StatelessWidget {
  const MeditationCard({
    required this.title,
    required this.image,
    this.route});

final String title;
final String image;
final String route;
...

I need the route variable as optional, but when I remove the flag, I get an error and ask me to make the variable mandatory.
Tried different approaches but didn’t work for me
This alert dialog

The parameter ‘route’ can’t have a value of ‘null’ because of its
type, but the implicit default value is ‘null’.

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

>Solution :

This happened because of null safety check.In order to set nullable variable, you should use ?, try this:

final String? route;
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