Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

flutter class constructors error - "@required this."

In the constructor of a class I get several errors because it says that a null value is assigned by default and that value cannot be allowed. I can't initialize them either because they have the final modified. I leave you the code and the error messages. Thanks in advance

Code:

{

  final IconData icon;
  final String placeholder;
  final TextEditingController textController;
  final TextInputType keyboardType;
  final bool isPassword;

  const CustomInput ({
      Key key,
      @required this.icon,
      @required this.placeholder,
      @required this.textController,
      this.keyboardType = TextInputType.text,
      this.isPassword = false
  }) : super(key:key);

Error:

lib/widgets/custom_input.dart:13:11: Error: The parameter 'key' can't have a value of 'null' because of its type 'Key', but the implicit default value is 'null'.
 - 'Key' is from 'package:flutter/src/foundation/key.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/foundation/key.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
      Key key,
          ^^^
lib/widgets/custom_input.dart:14:22: Error: The parameter 'icon' can't have a value of 'null' because of its type 'IconData', but the implicit default value is 'null'.
 - 'IconData' is from 'package:flutter/src/widgets/icon_data.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/widgets/icon_data.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
      @required this.icon,
                     ^^^^
lib/widgets/custom_input.dart:15:22: Error: The parameter 'placeholder' can't have a value of 'null' because of its type 'String', but the implicit default value is 'null'.
Try adding either an explicit non-'null' default value or the 'required' modifier.
      @required this.placeholder,
                     ^^^^^^^^^^^
lib/widgets/custom_input.dart:16:22: Error: The parameter 'textController' can't have a value of 'null' because of its type 'TextEditingController', but the implicit default value is 'null'.
 - 'TextEditingController' is from 'package:flutter/src/widgets/editable_text.dart' ('../../../snap/flutter/common/flutter/packages/flutter/lib/src/widgets/editable_text.dart').
Try adding either an explicit non-'null' default value or the 'required' modifier.
      @required this.textController,
                     ^^^^^^^^^^^^^^
like image 416
grafeno30 Avatar asked Oct 24 '25 14:10

grafeno30


1 Answers

remove the @

 {
  final IconData icon;
  final String placeholder;
  final TextEditingController textController;
  final TextInputType keyboardType;
  final bool isPassword;

  const CustomInput ({
      required Key key,
      required this.icon,
      required this.placeholder,
      required this.textController,
      this.keyboardType = TextInputType.text,
      this.isPassword = false
  }) : super(key:key);
like image 183
Huthaifa Muayyad Avatar answered Oct 26 '25 05:10

Huthaifa Muayyad