在 flutter 中将自定义对象作为屏幕参数传递时,常量值无效

问题描述 投票:0回答:1

我正在尝试将名为“Lesson”的自定义对象传递到名为“ExamScreen”的屏幕 这是电话:

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

和考试屏幕:

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 {

我收到错误

Invalid constant value

我该如何解决

flutter object parameter-passing
1个回答
0
投票

从 ExamScreen 构造函数中删除 const 关键字,如下所示:

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

  @override
  State<ExamScreen> createState() => _ExamScreenState();
}
© www.soinside.com 2019 - 2024. All rights reserved.