'Flutter: Generating routes with auto_route using a Class with named constructors instead of default constructor
I am trying to generate the router.gr.dart file using the auto_route package in flutter. My class has 3 named constructors:
ProfilePage.a({
Key? key,
this.profileType = UserType.a,
required this.a,
required this.userProfileId,
}) : super(key: key);
ProfilePage.b({
Key? key,
this.profileType = UserType.b,
required this.b,
required this.userProfileId,
}) : super(key: key);
ProfilePage.c({
Key? key,
this.profileType = UserType.c,
required this.c,
required this.userProfileId,
}) : super(key: key);
Now, when I try to generate the router.gr.dart file, it gives me an error:
[INFO] Generating build script...
[INFO] Generating build script completed, took 406ms
[INFO] Initializing inputs
[INFO] Reading cached asset graph...
[INFO] Reading cached asset graph completed, took 64ms
[INFO] Checking for updates since last build...
[INFO] Checking for updates since last build completed, took 651ms
[INFO] Running build...
[INFO] 1.1s elapsed, 0/2 actions completed.
[INFO] 2.2s elapsed, 0/2 actions completed.
[INFO] 10.4s elapsed, 0/2 actions completed.
[SEVERE] auto_route_generator:autoRouteGenerator on lib/routes/router.dart:
Null check operator used on a null value
[INFO] Running build completed, took 11.3s
[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 34ms
[SEVERE] Failed after 11.3s
pub finished with exit code 1
The generation works fine for classes with just the default constructor. How do I fix this problem?
Solution 1:[1]
Do some refactoring.
class ProfilePageA extends ProfilePage {
ProfilePageA({
Key? key,
UserType profileType = UserType.a,
required Object a,
required int userProfileId,
}) : super(
key: key,
profileType: profileType,
a: a,
userProfileId: userProfileId,
);
}
class ProfilePageB extends ProfilePage {
ProfilePageB({
Key? key,
UserType profileType = UserType.b,
required Object b,
required int userProfileId,
}) : super(
key: key,
profileType: profileType,
b: b,
userProfileId: userProfileId,
);
}
// etc
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | BambinoUA |