带有不匹配参数的闭包调用:函数'_RegisterState.build.<匿名闭包>'接收者:闭包:(字符串)=> Null
Posted
技术标签:
【中文标题】带有不匹配参数的闭包调用:函数\'_RegisterState.build.<匿名闭包>\'接收者:闭包:(字符串)=> Null【英文标题】:Closure call with mismatched arguments: function '_RegisterState.build.<anonymous closure>' Receiver: Closure: (String) => Null带有不匹配参数的闭包调用:函数'_RegisterState.build.<匿名闭包>'接收者:闭包:(字符串)=> Null 【发布时间】:2022-01-14 17:42:38 【问题描述】:我不知道为什么会发生这个错误,完整的错误是:
Closure call with mismatched arguments: function '_RegisterState.build.<anonymous closure>'
Receiver: Closure: (String) => Null
Tried calling: _RegisterState.build.<anonymous closure>()
Found: _RegisterState.build.<anonymous closure>(String) => Null
我认为我在此处创建的可重用 Textformfield 中的错误代码 我的可重用 TextformField 代码是:
import 'package:flutter/material.dart';
import '../constants.dart';
class CustomTextField extends StatelessWidget
const CustomTextField(
Key? key, required this.hint, required this.icon, required this.onClick)
: super(key: key);
final String hint;
final IconData icon;
final Function? onClick;
final OutlineInputBorder border = const OutlineInputBorder(
borderRadius: BorderRadius.all(Radius.circular(20)),
borderSide: BorderSide(color: Colors.white));
@override
Widget build(BuildContext context)
return Padding(
padding: const EdgeInsets.symmetric(horizontal: 30),
child: TextFormField(
onSaved: onClick!(),
validator: (value)
if (value!.isEmpty)
switch (hint)
case 'Enter your email':
return 'Enter your Email';
case 'Enter your Name':
return 'Enter a name';
case 'Enter your password':
return 'Enter a password';
default:
return 'Enter a value';
if (hint == 'Enter your email' && !value.contains('@'))
return 'Enter valid email';
if (hint == 'Enter your Name' && value.length < 3)
return 'Enter valid Name';
if (hint == 'Enter your password' && value.length < 5)
return 'your password is weak';
,
obscureText: hint == 'Enter your password' ? true : false,
cursorColor: kMainColor,
decoration: InputDecoration(
hintText: hint,
prefixIcon: Icon(
icon,
color: kMainColor,
),
filled: true,
fillColor: kSecondaryColor,
focusedBorder: border,
enabledBorder: border,
border: border,
),
),
);
我在 2 个屏幕中使用它来登录和注册
我的注册屏幕代码是:
import 'package:e_commerce/screens/login_screen.dart';
import 'package:e_commerce/servises/auth.dart';
import 'package:e_commerce/widgets/custom_input_field.dart';
import 'package:flutter/material.dart';
import '../constants.dart';
class Register extends StatefulWidget
static String routName = 'Register';
const Register(Key? key) : super(key: key);
@override
_RegisterState createState() => _RegisterState();
class _RegisterState extends State<Register>
String? name;
String? email;
String? password;
final GlobalKey<FormState> _key = GlobalKey();
@override
Widget build(BuildContext context)
double height = MediaQuery.of(context).size.height;
return Scaffold(
backgroundColor: kMainColor,
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.only(top: 100),
alignment: Alignment.center,
child: Form(
key: _key,
child: Column(
children: [
Image.asset("assets/images/icons/cart.png"),
const Text('Buy now',
style: TextStyle(
fontFamily: 'Corinthia',
color: Colors.black,
fontSize: 40,
fontWeight: FontWeight.bold)),
SizedBox(
height: height * 0.05,
),
CustomTextField(
onClick: (String value)
if (_key.currentState!.validate())
name = value;
,
hint: 'Enter your Name',
icon: Icons.person),
SizedBox(
height: height * 0.02,
),
CustomTextField(
onClick: (String value)
if (_key.currentState!.validate())
email = value;
,
hint: 'Enter your email',
icon: Icons.email_rounded),
SizedBox(
height: height * 0.02,
),
CustomTextField(
onClick: (String value)
if (_key.currentState!.validate())
password = value;
,
hint: 'Enter your password',
icon: Icons.lock),
SizedBox(
height: height * 0.05,
),
MaterialButton(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
color: Colors.black,
onPressed: () async
await MyAuth().sinIn(email!, password!);
,
child: const Text(
'Register',
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
SizedBox(
height: height * 0.02,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'have an account?',
style: TextStyle(fontSize: 20),
),
TextButton(
onPressed: ()
Navigator.of(context)
.pushReplacementNamed(LoginScreen.routName);
,
child: const Text(
'Login ',
style: TextStyle(fontSize: 20),
),
)
],
)
],
),
),
),
),
// ),
);
我的登录屏幕代码是:
import 'package:e_commerce/screens/register_screen.dart';
import 'package:e_commerce/servises/auth.dart';
import 'package:e_commerce/widgets/custom_input_field.dart';
import 'package:flutter/material.dart';
import 'package:e_commerce/constants.dart';
class LoginScreen extends StatefulWidget
static String routName = 'login';
const LoginScreen(Key? key) : super(key: key);
@override
State<LoginScreen> createState() => _LoginScreenState();
class _LoginScreenState extends State<LoginScreen>
final GlobalKey<FormState> _key = GlobalKey<FormState>();
String? name;
String? password;
String? email;
@override
Widget build(BuildContext context)
double height = MediaQuery.of(context).size.height;
return Scaffold(
backgroundColor: kMainColor,
body: SingleChildScrollView(
child: Container(
padding: const EdgeInsets.only(top: 100),
alignment: Alignment.center,
child: Column(
children: [
Image.asset("assets/images/icons/cart.png"),
const Text('Buy now',
style: TextStyle(
fontFamily: 'Corinthia',
color: Colors.black,
fontSize: 40,
fontWeight: FontWeight.bold)),
SizedBox(
height: height * 0.05,
),
CustomTextField(
onClick: (String value)
if (_key.currentState!.validate())
email = value;
,
hint: 'Enter your email',
icon: Icons.email_rounded),
SizedBox(
height: height * 0.02,
),
CustomTextField(
onClick: (String value)
if (_key.currentState!.validate())
password = value;
,
hint: 'Enter your password',
icon: Icons.lock),
SizedBox(
height: height * 0.05,
),
MaterialButton(
shape: const RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(8))),
color: Colors.black,
onPressed: ()
MyAuth().sinIn(email!, password!);
,
child: const Text(
'Login',
style: TextStyle(fontSize: 20, color: Colors.white),
),
),
SizedBox(
height: height * 0.02,
),
Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
const Text(
'Don\'t have account?',
style: TextStyle(fontSize: 20),
),
TextButton(
onPressed: ()
Navigator.of(context)
.pushReplacementNamed(Register.routName);
,
child: const Text(
'Register ',
style: TextStyle(fontSize: 20),
),
)
],
)
],
),
),
),
// ),
);
我使用了 Firebase 身份验证并创建了一个类来使用它我不知道它是否可能来自它的错误 我的身份验证是:
import 'package:firebase_auth/firebase_auth.dart';
class MyAuth
final _auth = FirebaseAuth.instance;
Future sinIn(String email, String password) async
final authResult = await _auth.signInWithEmailAndPassword(
email: email, password: password);
return authResult;
Future register(String email, String password) async
final authResult = await _auth.createUserWithEmailAndPassword(
email: email, password: password);
return authResult;
完整的错误是:
════════ Exception caught by widgets library ═══════════════════════════════════
Closure call with mismatched arguments: function '_RegisterState.build.<anonymous closure>'
Receiver: Closure: (String) => Null
Tried calling: _RegisterState.build.<anonymous closure>()
Found: _RegisterState.build.<anonymous closure>(String) => Null
The relevant error-causing widget was
CustomTextField
lib\screens\register_screen.dart:55
【问题讨论】:
【参考方案1】:看起来不匹配出现在 onClick
方法中,该方法在此处声明:
final Function? onClick; // it would help to further qualify Function here
你在这里调用它,不带参数:
onSaved: onClick!(), // note no parameter
但是当你将它传递给构造函数时,你传入的是Function(String)
:
onClick: (String value)
利用 Dart 的强类型来说明 onClick
是什么类型的函数。也许你的意思是:
final Function()? onClick;
或
final Function(String)? onClick;
另外,onSaved
我想你可能打算:
onSaved: () => onClick!(); // provide a function here that calls onClick
【讨论】:
以上是关于带有不匹配参数的闭包调用:函数'_RegisterState.build.<匿名闭包>'接收者:闭包:(字符串)=> Null的主要内容,如果未能解决你的问题,请参考以下文章
register_shutdown_function 函数详解