Invalid constant value when passing custom object as a screen parameter in flutter

26 views Asked by At

i am trying to pass a custom object called "Lesson" to a screen called "ExamScreen" here is the call:

goStepScreen(int statusId, Lesson lesson, BuildContext context) {
  if (statusId == SessionStates.Revision.getStatus()) {
    navigateTo(context, const ExamScreen(lesson: lesson,));

and the exam screen:

class ExamScreen extends StatefulWidget {
  final Lesson? lesson;
  const ExamScreen({super.key,  this.lesson});

  @override
  State<ExamScreen> createState() => _ExamScreenState();
}

class _ExamScreenState extends State<ExamScreen> with WidgetsBindingObserver {

i get error Invalid constant value

how can i solve it

2

There are 2 answers

0
Vladimir Gadzhov On BEST ANSWER

Remove the const keyword from the navigateTo

navigateTo(context, const ExamScreen(lesson: lesson,));

You receive this error because lesson isn't a constant value, so you cannot create a constant instance.

0
Yes Dev On

Remove the const keyword from the ExamScreen constructor like this:

class ExamScreen extends StatefulWidget {
  final Lesson? lesson;
  ExamScreen({super.key,  this.lesson}); // Here

  @override
  State<ExamScreen> createState() => _ExamScreenState();
}