Home > Net >  The named parameter 'semesters' is required, but there's no corresponding argument
The named parameter 'semesters' is required, but there's no corresponding argument

Time:12-04

main.dart `

void main() {
  runApp(
    Provider(
      create: (_) => LoginService(),
      child: MaterialApp(
        theme: ThemeData(fontFamily: 'Roboto'),
        debugShowCheckedModeBanner: false,
        routes: {
          '/': (context) => SplashPage(goToPage: WelcomePage(), duration: 3),
          '/welcomepage' : (context) => WelcomePage(),
          '/categorylistpage' : (context) => CategoryListPage(),
          '/selectedcategorypage' : (context) => SelectedCategoryPage(),
        }
      ),
    )
  );
}

`

selectedcategorypage.dart `

class SelectedCategoryPage extends StatelessWidget {
  final MyCategory? selectedCategory;
  final List<MySemester> semesters;
  const SelectedCategoryPage({
    Key? key,
    this.selectedCategory,
    required this.semesters,
  }) : super(key: key);

`

I tried to make the routs but for selectedcategory I have required this.semesters which I don't need a required to but without it I get this error: The parameter 'semesters' can't have a value of 'null' because of its type, but the implicit default value is 'null'. I don't know how to get around it

CodePudding user response:

You can provide a default value for the semesters parameter so that it is not required. You can do this by using the = operator after the parameter type and providing a default value, like this:

class SelectedCategoryPage extends StatelessWidget {
  final MyCategory? selectedCategory;
  final List<MySemester> semesters = [];
  const SelectedCategoryPage({
    Key? key,
    this.selectedCategory,
    required this.semesters,
  }) : super(key: key);
}

Now, if the semesters parameter is not provided, it will default to an empty list. You can also use a default value of null if you want to allow the value to be null and handle that case in your code.

CodePudding user response:

If the parameter 'semesters' is not required, then just remove the 'required' tag and make the variable nullable. Like:

 final List<MySemester>? semesters;

Full code:

class SelectedCategoryPage extends StatelessWidget {
  final MyCategory? selectedCategory;
  final List<MySemester>? semesters;
  const SelectedCategoryPage({
    Key? key,
    this.selectedCategory,
    this.semesters,
  }) : super(key: key);
  • Related