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
By writing
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
so change that to
for example or make it not required by providing a default value like
The way you called the constructor, all the three fields are required and you have initialize all of them. just replace this
by
This will give the default value to obscure (false) unless you initialize it. Hopefully this will remove your error.
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 calledobscured
, along with other required parameters such as:labletext
andInputcontroller
.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: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: