TypeError: Cannot read properties of null (reading 'email')
TypeError: Cannot read properties of null (reading 'email')
有人知道我为什么会收到这个错误吗?我正在使用 final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController();
从文本字段(密码和电子邮件)中获取文本。
然后我将 signIn() 方法与电子邮件一起使用:_emailController.text.trim(), password: _passwordController.text.trim()
从文本字段中获取字符串。此信息用于登录用户。
按钮登录只使用methodde signIn() 然后显示页面仪表板。在页面仪表板上有一个包含登录用户电子邮件的文本。
为什么会出现这个错误?我哪里做错了?
错误信息:
======== Exception caught by widgets library =======================================================
The following JSNoSuchMethodError was thrown building DashboardView(dirty, state: _DashboardViewState#00d1f):
TypeError: Cannot read properties of null (reading 'email')
The relevant error-causing widget was:
DashboardView DashboardView:file:///C:/Users/Dennis/Documents/bestfitnesstrackereu/lib/routing/router.dart:25:28
When the exception was thrown, this was the stack:
packages/bestfitnesstrackereu/pages/dashboard/dashboard_view.dart 50:1 build
packages/flutter/src/widgets/framework.dart 4922:27 build
authentication.dart代码:(登录)
import 'package:bestfitnesstrackereu/routing/route_names.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class AuthenticationPage extends StatefulWidget {
const AuthenticationPage({Key key}) : super(key: key);
@override
State<AuthenticationPage> createState() => _AuthenticationPageState();
}
class _AuthenticationPageState extends State<AuthenticationPage> {
bool checkBoxValue = false;
//textfield controllers:
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
Future signIn() async{
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: _emailController.text.trim(),
password: _passwordController.text.trim()
);
} on FirebaseAuthException catch (e){
print(e);
}
}
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
constraints: BoxConstraints(maxWidth: 400),
padding: EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _emailController,
decoration: InputDecoration(
labelText: "E-Mail",
hintText: "abc@domain.com",
suffixIcon: Icon(Icons.mail_outline,),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20)
)
),
),
SizedBox(height: 15,),
TextField(
controller: _passwordController,
obscureText: true,
decoration: InputDecoration(
labelText: "Password",
hintText: "******",
suffixIcon: Icon(Icons.lock_outline, color: Colors.grey,),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20)
)
),
),
InkWell(
onTap: (){
signIn();
Navigator.of(context).pushNamed(DashboardRoute);
},
child: Container(
decoration: BoxDecoration(color: Colors.deepPurple,
borderRadius: BorderRadius.circular(20)),
alignment: Alignment.center,
width: double.maxFinite,
padding: EdgeInsets.symmetric(vertical: 16),
child: Text(
"Login",
style: TextStyle(
color: Colors.white,
),)
)
),
],
),
)
),
);
}
}
dashboard_view.dart代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import '../../routing/route_names.dart';
class DashboardView extends StatefulWidget {
const DashboardView ({Key key}) : super(key: key);
@override
State<DashboardView> createState() => _DashboardViewState();
}
class _DashboardViewState extends State<DashboardView> {
final user = FirebaseAuth.instance.currentUser;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Dashboard View - signed in as '),
SizedBox(height: 8,),
Text(user.email),
InkWell(
onTap: () async {
await FirebaseAuth.instance.signOut();
print('user ist ausgeloggt');
//Navigator.of(context).pushNamed(AuthenticationPageRoute);
},
child: Container(
decoration: BoxDecoration(color: Colors.deepPurple,
borderRadius: BorderRadius.circular(20)),
alignment: Alignment.center,
width: double.maxFinite,
padding: EdgeInsets.symmetric(vertical: 16),
child: Text(
'sign out',
style: TextStyle(
color: Colors.white,
),)
)
),
],
),
),
);
}
}
你在做
onTap: (){
signIn();
Navigator.of(context).pushNamed(DashboardRoute);
},
由于您没有等待 signIn()
方法完成,您已经导航到 DashboardRoute
并且 _emailController
已处理。
这就是您遇到问题的原因。
在导航到下一个屏幕之前尝试等待标志的响应
onTap: () async {
await signIn();
Navigator.of(context).pushNamed(DashboardRoute);
},
有人知道我为什么会收到这个错误吗?我正在使用 final TextEditingController _emailController = TextEditingController(); final TextEditingController _passwordController = TextEditingController();
从文本字段(密码和电子邮件)中获取文本。
然后我将 signIn() 方法与电子邮件一起使用:_emailController.text.trim(), password: _passwordController.text.trim()
从文本字段中获取字符串。此信息用于登录用户。
按钮登录只使用methodde signIn() 然后显示页面仪表板。在页面仪表板上有一个包含登录用户电子邮件的文本。
为什么会出现这个错误?我哪里做错了?
错误信息:
======== Exception caught by widgets library =======================================================
The following JSNoSuchMethodError was thrown building DashboardView(dirty, state: _DashboardViewState#00d1f):
TypeError: Cannot read properties of null (reading 'email')
The relevant error-causing widget was:
DashboardView DashboardView:file:///C:/Users/Dennis/Documents/bestfitnesstrackereu/lib/routing/router.dart:25:28
When the exception was thrown, this was the stack:
packages/bestfitnesstrackereu/pages/dashboard/dashboard_view.dart 50:1 build
packages/flutter/src/widgets/framework.dart 4922:27 build
authentication.dart代码:(登录)
import 'package:bestfitnesstrackereu/routing/route_names.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
class AuthenticationPage extends StatefulWidget {
const AuthenticationPage({Key key}) : super(key: key);
@override
State<AuthenticationPage> createState() => _AuthenticationPageState();
}
class _AuthenticationPageState extends State<AuthenticationPage> {
bool checkBoxValue = false;
//textfield controllers:
final TextEditingController _emailController = TextEditingController();
final TextEditingController _passwordController = TextEditingController();
Future signIn() async{
try {
await FirebaseAuth.instance.signInWithEmailAndPassword(
email: _emailController.text.trim(),
password: _passwordController.text.trim()
);
} on FirebaseAuthException catch (e){
print(e);
}
}
@override
void dispose() {
_emailController.dispose();
_passwordController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Container(
constraints: BoxConstraints(maxWidth: 400),
padding: EdgeInsets.all(24),
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
TextField(
controller: _emailController,
decoration: InputDecoration(
labelText: "E-Mail",
hintText: "abc@domain.com",
suffixIcon: Icon(Icons.mail_outline,),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20)
)
),
),
SizedBox(height: 15,),
TextField(
controller: _passwordController,
obscureText: true,
decoration: InputDecoration(
labelText: "Password",
hintText: "******",
suffixIcon: Icon(Icons.lock_outline, color: Colors.grey,),
border: OutlineInputBorder(
borderRadius: BorderRadius.circular(20)
)
),
),
InkWell(
onTap: (){
signIn();
Navigator.of(context).pushNamed(DashboardRoute);
},
child: Container(
decoration: BoxDecoration(color: Colors.deepPurple,
borderRadius: BorderRadius.circular(20)),
alignment: Alignment.center,
width: double.maxFinite,
padding: EdgeInsets.symmetric(vertical: 16),
child: Text(
"Login",
style: TextStyle(
color: Colors.white,
),)
)
),
],
),
)
),
);
}
}
dashboard_view.dart代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import '../../routing/route_names.dart';
class DashboardView extends StatefulWidget {
const DashboardView ({Key key}) : super(key: key);
@override
State<DashboardView> createState() => _DashboardViewState();
}
class _DashboardViewState extends State<DashboardView> {
final user = FirebaseAuth.instance.currentUser;
@override
Widget build(BuildContext context) {
return Scaffold(
body: Center(
child: Column(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('Dashboard View - signed in as '),
SizedBox(height: 8,),
Text(user.email),
InkWell(
onTap: () async {
await FirebaseAuth.instance.signOut();
print('user ist ausgeloggt');
//Navigator.of(context).pushNamed(AuthenticationPageRoute);
},
child: Container(
decoration: BoxDecoration(color: Colors.deepPurple,
borderRadius: BorderRadius.circular(20)),
alignment: Alignment.center,
width: double.maxFinite,
padding: EdgeInsets.symmetric(vertical: 16),
child: Text(
'sign out',
style: TextStyle(
color: Colors.white,
),)
)
),
],
),
),
);
}
}
你在做
onTap: (){
signIn();
Navigator.of(context).pushNamed(DashboardRoute);
},
由于您没有等待 signIn()
方法完成,您已经导航到 DashboardRoute
并且 _emailController
已处理。
这就是您遇到问题的原因。
在导航到下一个屏幕之前尝试等待标志的响应
onTap: () async {
await signIn();
Navigator.of(context).pushNamed(DashboardRoute);
},