Error: The parameter 'onSubmit' can't have a value

Error: The parameter 'onSubmit' can't have a value

您好,我在一个名为 components 的单独 dart 文件中创建了一个默认表单域,我也有 login_screen.dart。 有很多错误我不知道如何修复它,并使代码工作,我将放下 component.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,
        required 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,
  Function onSubmit,
  Function onChange,
  required Function validate,
  required var label,
  required IconData prefix,
}) =>
    TextFormField(
      controller: controller,
      keyboardType: type,
      onFieldSubmitted: onSubmit(),
      onChanged: onChange(),
      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 中的空安全特性,我不知道,因为我是这项技术的新手。

为什么

发生这种情况的原因是因为启用了空安全,您的函数 onSubmitonChange 不能为空。

解决方案

我会这样做:

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