LateInitializationError: Field '_email@25234523' has already been initialized
LateInitializationError: Field '_email@25234523' has already been initialized
我是 Flask 新手。我在登录和注册页面之间切换时收到此异常。有人可以帮忙吗?
下面添加了登录和注册页面的代码。
注册码
'''
class RegisterView 扩展了 StatefulWidget {
const RegisterView({Key?key}) : super(key: key);
@override
State<RegisterView> createState() => _RegisterViewState();
}
class _RegisterViewState extends State<RegisterView> {
late final TextEditingController _email;
late final TextEditingController _password;
@override
void initState() {
_email = TextEditingController();
_password = TextEditingController();
super.initState();
}
@override
void dispose() {
_email = TextEditingController();
_password = TextEditingController();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Register'),
),
body: Column(
children: [
TextField(
controller: _email,
obscureText: false,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: 'Enter your email here',
),
),
TextField(
controller: _password,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
decoration: const InputDecoration(
hintText: 'Enter your password here',
),
),
TextButton(
onPressed: () async {
final email = _email.text;
final password = _password.text;
try {
// ignore: non_constant_identifier_names
final UserCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
print(UserCredential);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('Weak password');
} else if (e.code == 'email-already-in-use') {
print('Email is already in use');
} else if (e.code == 'invalid-email') {
print('Invalid email');
}
} catch (e) {
print('Something bad happened');
}
},
child: const Text('Register'),
),
TextButton(
onPressed: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/login',
(route) => false
);
},
child: const Text('Already registered? Login here!'),
),
],
),
);
}
}
'''
登录代码
'''
class LoginView 扩展了 StatefulWidget {
const LoginView({Key?key}) : super(key: key);
@override
State<LoginView> createState() => _LoginViewState();
}
class _LoginViewState extends State<LoginView> {
late final TextEditingController _email;
late final TextEditingController _password;
@override
void initState() {
_email = TextEditingController();
_password = TextEditingController();
super.initState();
}
@override
void dispose() {
_email = TextEditingController();
_password = TextEditingController();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
),
body: Column(
children: [
TextField(
controller: _email,
obscureText: false,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: 'Enter your email here',
),
),
TextField(
controller: _password,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
decoration: const InputDecoration(
hintText: 'Enter your password here',
),
),
TextButton(
onPressed: () async {
final email = _email.text;
final password = _password.text;
try {
// ignore: non_constant_identifier_names
final UserCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
print(UserCredential);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('User not found');
} else if (e.code == 'wrong-password') {
print('Wrong Password');
}
} catch (e) {
print('Something bad happened');
print(e.runtimeType);
print(e);
}
},
child: const Text('Login'),
),
TextButton(
onPressed: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/register',
(route) => false
);
},
child: const Text('Not registered yet? Register here!'),
),
],
),
);
}
}
'''
查看 dispose 方法,你正在再次初始化控制器,因为我猜是复制粘贴代码
应该是
@override
void dispose() {
_email.dispose();
_password.dispose();
super.dispose();
}
我是 Flask 新手。我在登录和注册页面之间切换时收到此异常。有人可以帮忙吗? 下面添加了登录和注册页面的代码。
注册码
'''
class RegisterView 扩展了 StatefulWidget {
const RegisterView({Key?key}) : super(key: key);
@override
State<RegisterView> createState() => _RegisterViewState();
}
class _RegisterViewState extends State<RegisterView> {
late final TextEditingController _email;
late final TextEditingController _password;
@override
void initState() {
_email = TextEditingController();
_password = TextEditingController();
super.initState();
}
@override
void dispose() {
_email = TextEditingController();
_password = TextEditingController();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Register'),
),
body: Column(
children: [
TextField(
controller: _email,
obscureText: false,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: 'Enter your email here',
),
),
TextField(
controller: _password,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
decoration: const InputDecoration(
hintText: 'Enter your password here',
),
),
TextButton(
onPressed: () async {
final email = _email.text;
final password = _password.text;
try {
// ignore: non_constant_identifier_names
final UserCredential = await FirebaseAuth.instance.createUserWithEmailAndPassword(
email: email,
password: password,
);
print(UserCredential);
} on FirebaseAuthException catch (e) {
if (e.code == 'weak-password') {
print('Weak password');
} else if (e.code == 'email-already-in-use') {
print('Email is already in use');
} else if (e.code == 'invalid-email') {
print('Invalid email');
}
} catch (e) {
print('Something bad happened');
}
},
child: const Text('Register'),
),
TextButton(
onPressed: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/login',
(route) => false
);
},
child: const Text('Already registered? Login here!'),
),
],
),
);
}
}
'''
登录代码
''' class LoginView 扩展了 StatefulWidget { const LoginView({Key?key}) : super(key: key);
@override
State<LoginView> createState() => _LoginViewState();
}
class _LoginViewState extends State<LoginView> {
late final TextEditingController _email;
late final TextEditingController _password;
@override
void initState() {
_email = TextEditingController();
_password = TextEditingController();
super.initState();
}
@override
void dispose() {
_email = TextEditingController();
_password = TextEditingController();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: const Text('Login'),
),
body: Column(
children: [
TextField(
controller: _email,
obscureText: false,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.emailAddress,
decoration: const InputDecoration(
hintText: 'Enter your email here',
),
),
TextField(
controller: _password,
obscureText: true,
enableSuggestions: false,
autocorrect: false,
keyboardType: TextInputType.visiblePassword,
decoration: const InputDecoration(
hintText: 'Enter your password here',
),
),
TextButton(
onPressed: () async {
final email = _email.text;
final password = _password.text;
try {
// ignore: non_constant_identifier_names
final UserCredential = await FirebaseAuth.instance.signInWithEmailAndPassword(
email: email,
password: password,
);
print(UserCredential);
} on FirebaseAuthException catch (e) {
if (e.code == 'user-not-found') {
print('User not found');
} else if (e.code == 'wrong-password') {
print('Wrong Password');
}
} catch (e) {
print('Something bad happened');
print(e.runtimeType);
print(e);
}
},
child: const Text('Login'),
),
TextButton(
onPressed: () {
Navigator.of(context).pushNamedAndRemoveUntil(
'/register',
(route) => false
);
},
child: const Text('Not registered yet? Register here!'),
),
],
),
);
}
}
'''
查看 dispose 方法,你正在再次初始化控制器,因为我猜是复制粘贴代码
应该是
@override
void dispose() {
_email.dispose();
_password.dispose();
super.dispose();
}