Search code examples
flutterdarttextfield

The named parameter 'obscured' is required, but there's no corresponding argument. Try adding the required argument


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),
                  ],
                ))
              ],
            ),
          ),
        );
      }
    }
  

Solution

  • 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});