I am creating some classes but I am getting this error message:
Constructors for public widgets should have a named ‘key’ parameter.
I have tried to add the key but it is not working.
const TitleCompanyScreen([this.isNewTitleCompany, this.titleCompanyId], {super.key});
How do I add the named ‘key’ parameter to a class?
UPDATE:
I made changes but I am still getting errors.
class TitleCompanyScreen extends ConsumerStatefulWidget {
final bool? isNewTitleCompany;
final String? titleCompanyId;
const TitleCompanyScreen({[this.isNewTitleCompany, this.titleCompanyId]});
@override
ConsumerState<TitleCompanyScreen> createState() => _TitleCompanyScreenState();
}
If I don’t initialize the final variables I get this error:
All final variables must be initialized, but 'isNewTitleCompany' and 'titleCompanyId' aren't.
Try adding initializers for the fields.
If I initialize the variables to false and ” respectively I get this error. the error is marked on the first [ in the line below:
const TitleCompanyScreen({[this.isNewTitleCompany, this.titleCompanyId]});
Expected an identifier.dart(missing_identifier)
Expected to find '}'.
2
Answers
Please use {} to surround the named parameters.
TitleCompanyScreen({this.isNewTitleCompany, this.titleCompanyId})
In Dart, a class constructor cannot simultaneously accept positional parameters
(such as
[this.isNewTitleCompany, this.titleCompanyId]
) and named parameters(such as
{super.key}
) without defining them as either required or optional innamed form. This is due to the syntactic constraints of the Dart language,
designed to ensure clarity and prevent confusion when calling constructors
with multiple parameters.