【问题标题】:Error: Expected ',' before this. validator: validate!(String value),错误:在此之前应为“,”。验证器:验证!(字符串值),
【发布时间】:2021-10-30 07:51:17
【问题描述】:

您好,我在 Flutter 中遇到此错误:

lib/shared/components/components.dart:41:35: Error: Expected ',' before this.
      validator: validate!(String value),
                                  ^^^^^
lib/modules/login/login_screen.dart:36:33: Error: The argument type 'String? Function(String)' can't be assigned to the parameter type 'dynamic Function()?'.
                      validate: (String value){
                                ^
lib/shared/components/components.dart:41:35: Error: Getter not found: 'value'.
      validator: validate!(String value),
                                  ^^^^^
lib/shared/components/components.dart:41:27: Error: Too many positional arguments: 0 allowed, but 2 found.
Try removing the extra positional arguments.
      validator: validate!(String value),
                          ^


FAILURE: Build failed with an exception.

* Where:
Script 'C:\src\flutter\packages\flutter_tools\gradle\flutter.gradle' line: 1005

* What went wrong:
Execution failed for task ':app:compileFlutterBuildDebug'.
> Process 'command 'C:\src\flutter\bin\flutter.bat'' finished with non-zero exit value 1

* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output. Run with --scan to get full insights.

components.dart 代码为:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';

Widget defaultButton(
    {double width = double.infinity,
      Color background = Colors.blue,
      double radius = 10.0,
      Function()? function,
      required String text,
      bool isUpperCase = true}) =>
    Container(
      height: 40.0,
      width: width,
      child: MaterialButton(
          onLongPress: () {},
          onPressed: function!() ,
          child: Text(
            isUpperCase ? text.toUpperCase() : text.toLowerCase(),
            style: TextStyle(color: Colors.white),
          )),
      decoration: BoxDecoration(
        borderRadius: BorderRadiusDirectional.circular(radius),
        color: background,
      ),
    );

Widget defaultFormFeild({
  required TextEditingController controller,
  required TextInputType type,
  void Function(String)? onSubmit,
  void Function(String)? onChange,
  required Function()? validate,
  required var label,
  required IconData prefix,
}) =>
    TextFormField(
      controller: controller,
      keyboardType: type,
      onFieldSubmitted: onSubmit, //do null checking
      onChanged: onChange, //do null checking
      validator: validate!(),
      decoration: InputDecoration(
          labelText: label,
          prefixIcon: Icon(prefix),
          border: OutlineInputBorder()
      ),
    );

login_screen.dart 代码:

import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:udemy_flutter/shared/components/components.dart';
class LoginScreen extends StatelessWidget {
  var emailController = TextEditingController();
  var passController = TextEditingController();
  var formKey = GlobalKey<FormState>();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Center(
          child: SingleChildScrollView(
            child: Form(
              key: formKey,
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.start,
                children: [
                  const Text(
                      'Login',
                    style: TextStyle(
                      fontSize: 40.0,
                      fontWeight: FontWeight.bold
                    ),
                  ),
                  const SizedBox(
                    height: 40.0,
                  ),
                  defaultFormFeild(
                      controller: emailController,
                      label: 'Email',
                      prefix: Icons.email,
                      type: TextInputType.emailAddress,
                      validate: (String value){
                        if(value.isEmpty != null){
                          return 'Email Cannot Be Empty';
                        }
                        return null;
                      }
                  ),
                  const SizedBox(
                    height: 15.0,
                  ),
                  TextFormField(
                    controller: passController,
                    obscureText: true,
                    keyboardType: TextInputType.visiblePassword,
                    decoration: const InputDecoration(
                      border: OutlineInputBorder(),
                      labelText: 'Password',
                      prefixIcon: Icon(
                        Icons.lock
                      ),
                      suffixIcon: Icon(
                        Icons.remove_red_eye,
                      )
                    ),
                    onChanged: (value) {
                      print(value);
                    },
                    onFieldSubmitted: (value) {
                      print(value);
                    },
                    validator: (value) {
                      if(value!.isEmpty){
                        return 'Password cannot be empty';
                      }
                      return null;
                    },
                  ),
                  const SizedBox(
                    height: 10.0,
                  ),
                  defaultButton(
                    function: (){
                      print(emailController.text);
                      print(passController.text);
                    },
                    text: 'Login',
                  ),
                  const SizedBox(
                    height: 10.0,
                  ),
                  defaultButton(
                    text: 'ReGister',
                    function: () {
                      print('You have just clicked on register');
                    },
                    background: Colors.red,
                    isUpperCase: false
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: [
                      const Text('Don\'t you have an account?'),
                      TextButton(onPressed: () {}, child: const Text(
                        'Register Now'
                      ))
                    ],
                  )
                ],
              ),
            ),
          ),
        ),
      ),
    );
  }
}

谁能告诉我哪里出错了我是 Flutter 平台的新手,我正在尝试构建各种用途的屏幕,因为我遇到了问题使用 Null 安全性,我设法解决了它,但我不知道这段代码到底有什么问题

【问题讨论】:

    标签: flutter flutter-layout dart-null-safety null-safety


    【解决方案1】:

    TextFormField 接受 FormFieldValidator?作为validator。 FormFieldValidator的定义是:

    typedef FormFieldValidator<T> = String? Function(T? value);
    

    这与您对Function()? validator 的定义不符。您需要更改您的类型以匹配 FormFieldValidator。

      void Function(String)? onChange,
      required Function()? validate,
      required var label,
    

    会变成

      void Function(String)? onChange,
      required String Function(String)? validate
      required var label,
    

    接下来,还有第二个问题。您在分配它之前正在执行此功能。这是不必要的,并且会导致类型不匹配。在defaultFormFeild 中将代码更改为在将验证器传递给表单字段之前不执行验证器。

          onChanged: onChange, //do null checking
          validator: validate,
          decoration: InputDecoration(
    

    【讨论】:

    • 非常感谢!!!
    猜你喜欢
    • 2020-08-18
    • 1970-01-01
    • 2016-08-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-10
    • 2013-03-05
    • 1970-01-01
    相关资源
    最近更新 更多