skip to Main Content

MyTextFields I get this message that a parameter named ‘obscured’ is required, but there is no corresponding argument.
Try adding the required arguments. I’ve tried to fix it for a while, but the problem is still the same. I put data into mysql. I watched this video code from youtube, but the tutorial can run normally without any problem.

   MyTextFields.dart
      class MyTextFields extends StatelessWidget {
      MyTextFields(
          {super.key,
          required this.lableltext,
          required this.obscured,
          required this.Inputcontroller});

      final String lableltext;
      final bool obscured;
      final TextEditingController Inputcontroller;

      @override
      Widget build(BuildContext context) {
        return Padding(
          padding: const EdgeInsets.symmetric(horizontal: 20.0),
          child: TextFormField(
            obscureText: obscured,
            controller: Inputcontroller,
            validator: (value) {
              if (value!.isEmpty) {
                return "$lableltext is required";
              }
            },
            decoration: InputDecoration(
              labelText: lableltext,
              enabledBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 3, color: Colors.blueAccent)),
              focusedBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 3, color: Colors.blueAccent)),
              errorBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 3, color: Colors.blueAccent)),
             focusedErrorBorder: OutlineInputBorder(
                  borderSide: BorderSide(width: 3, color: Colors.blueAccent)),
            ),
          ),
        );
      }
    }



report.dart
    class report_problem extends StatefulWidget {
      @override
      _report_problemState createState() => _report_problemState();
    }

    class _report_problemState extends State<report_problem> {
      TextEditingController name_surname = TextEditingController();
      TextEditingController address = TextEditingController();
      TextEditingController phone_number = TextEditingController();
      TextEditingController area = TextEditingController();

      @override
      Widget build(BuildContext context) {
       return Scaffold(
          appBar: AppBar(
            backgroundColor: Color.fromARGB(255, 14, 12, 134),
            title: const Text('report'),
          ),
          body: SingleChildScrollView(
            child: Column(
              children: [
                SizedBox(height: 30.0),
                Form(
                    child: Column(
                    children: [
         //Error           MyTextFields(
                        lableltext: "name:", Inputcontroller: name_surname),
                    SizedBox(height: 10.0),
                    MyTextFields(lableltext: "address:", Inputcontroller: address),
                    SizedBox(height: 10.0),
                    MyTextFields(
                        lableltext: "number:", Inputcontroller: phone_number),
                    SizedBox(height: 10.0),
                    MyTextFields(
                        lableltext: "location:",
                        Inputcontroller: area),
                  ],
                ))
              ],
            ),
          ),
        );
      }
    }
  

3

Answers


  1. By writing

      MyTextFields(
          {super.key,
          required this.lableltext,
          required this.obscured,
          required this.Inputcontroller});
    

    you literally say that you have to fill in the obscured parameter wherever you call the constructor, but you aren’t doing that for example at

    MyTextFields(lableltext: "address:", Inputcontroller: address)
    

    so change that to

    MyTextFields(lableltext: "address:", obscured: false, Inputcontroller: address)
    

    for example or make it not required by providing a default value like

      MyTextFields(
          {super.key,
          required this.lableltext,
          this.obscured = false,
          required this.Inputcontroller});
    
    Login or Signup to reply.
  2. The way you called the constructor, all the three fields are required and you have initialize all of them. just replace this

    MyTextFields(
          {super.key,
          required this.lableltext,
          required this.obscured,
          required this.Inputcontroller});
    

    by

    MyTextFields(
          {super.key,
          required this.lableltext,
          this.obscured = false,
          required this.Inputcontroller}); 
    

    This will give the default value to obscure (false) unless you initialize it. Hopefully this will remove your error.

    Login or Signup to reply.
  3. There’s some approaches to solve your problem. The way you built your MyTextFields Widget indicates to Dart that to Instantiate your class you’ll need a required boolean parameter called obscured, along with other required parameters such as: labletext and Inputcontroller.

    I can imagine that you don’t want to always set this parameter on the MyTextFields Widget contructor to avoid unnecessary text in your code. So, you can solve by adding a default value to obscured on MyTextFields constructor like this:

    class MyTextFields extends StatelessWidget {
      MyTextFields(
          {super.key,
          this.obscured = false,
          required this.lableltext,
          required this.Inputcontroller});
    
      final String lableltext;
      final bool obscured;
      final TextEditingController Inputcontroller;
    

    It will let you keep your code the way it already is, but when you want to use your MyTextFields widget with obscured text, you can only add this parameter where you instantiate It. Like this:

    MyTextFields(
      lableltext: "name:", Inputcontroller: name_surname, obscured: true),
    SizedBox(height: 10.0),
    MyTextFields(lableltext: "address:", Inputcontroller: address),
    SizedBox(height: 10.0),
    MyTextFields(
      lableltext: "number:", Inputcontroller: phone_number, obscured: true),
    SizedBox(height: 10.0),
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search