flutter 命名参数“obscured”是必需的,但没有相应的参数,尝试添加所需的参数

d7v8vwbk  于 2023-06-30  发布在  Flutter
关注(0)|答案(3)|浏览(166)

MyTextFields我得到一条消息,需要一个名为“obscured”的参数,但没有相应的参数。尝试添加所需的参数。我已经试着修复了一段时间,但问题还是一样。我把数据放到mysql里。我从youtube上看了这个视频代码,但是教程可以正常运行,没有任何问题。

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

g6baxovj1#

通过写作

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

你字面上说你必须在调用构造函数的任何地方填充隐藏的参数,但你没有这样做,例如在

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

所以改成

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

或通过提供默认值使其不需要,如

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

uajslkp62#

按照调用构造函数的方式,所有三个字段都是必需的,并且已经初始化了它们。把这个换掉

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

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

这将给予默认值为obscure(false),除非您对其进行初始化。希望这能消除你的错误。

7y4bm7vi

7y4bm7vi3#

有一些方法可以解决你的问题。构建MyTextFields Widget的方式向Dart表明,要示例化类,需要一个布尔参数obscured,沿着其他必要的参数,例如:labletextInputcontroller
我可以想象,您不希望总是在MyTextFields Widget构造器上设置这个参数,以避免代码中出现不必要的文本。所以,你可以通过在MyTextFields构造函数上向obscured添加一个默认值来解决问题,如下所示:

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;

它可以让您保持代码原来的样子,但是当您想使用MyTextFields小部件时,您只能在示例化它的地方添加此参数。像这样:

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),

相关问题