[英]Flutter method 'resetPassword' was called on null
我試圖創建一個按鈕,允許用戶重置他們的密碼。 但是,當我使用該方法時,它是在 null 上調用的。
我不知道我哪里做錯了。 我檢查了所有內容,它應該可以工作,因為我已經在名為 BaseAuth 的抽象類中實現了該方法。
有人可以幫我嗎?
這就是我如何使用 ResetPasswordPage
@override
Widget build(BuildContext context) {
_isIos = Theme.of(context).platform == TargetPlatform.iOS;
return new Scaffold(
appBar: new AppBar(
title: new Text("Organizer"),
backgroundColor: Colors.blueAccent,
),
body: Stack(
children: <Widget>[
_showBody(),
_showCircularProgress(),
],
));
}
Widget _showBody(){
return new Container(
padding: EdgeInsets.all(10.0),
child: new Form(
key: _formKey,
child: new ListView(
shrinkWrap: true,
children: <Widget>[
_showEmailInput(),
_showPasswordInput(),
_showPrimaryButton(),
_showSecondaryButton(),
_resetPassword(),
],
),
));
}
Widget _resetPassword() {
return new Padding(
padding: EdgeInsets.fromLTRB(0.0, 0.0, 0.0, 0.0),
child : FlatButton(
child: new Text('Forget password?',
style:
new TextStyle(fontSize: 18.0, fontWeight: FontWeight.w300)),
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ResetPasswordPage()),
);
}
)
);
}
這是 ResetPasswordPage 類
class ResetPasswordPage extends StatefulWidget {
ResetPasswordPage({Key key, this.auth});
final BaseAuth auth;
@override
_ResetPasswordPageState createState() => _ResetPasswordPageState();
}
class _ResetPasswordPageState extends State<ResetPasswordPage> {
String _email = '';
TextEditingController _emailController;
@override
void initState() {
super.initState();
final _emailController = TextEditingController(text: _email);
}
@override
void dispose() {
// Clean up the controller when the widget is disposed.
_emailController.dispose();
super.dispose();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: new AppBar(
title: new Text('Reset Password Page'),
backgroundColor: Colors.red,
),
body: Container(
padding: EdgeInsets.all(10.0),
child: ListView(
children: <Widget>[
new Padding(
padding: const EdgeInsets.fromLTRB(0.0, 100.0, 0.0, 0.0),
child: new TextField(
maxLines: 1,
keyboardType: TextInputType.emailAddress,
decoration: new InputDecoration(
hintText: 'Email',
icon: new Icon(
Icons.mail,
color: Colors.grey,
)),
onChanged: (value) {
_email = value;
},
controller: _emailController,
),
),
new Padding(
padding: const EdgeInsets.fromLTRB(30.0, 20.0, 30.0, 0.0),
child: RaisedButton(
child: new Text('Sumbit',
style: new TextStyle(
fontSize: 18.0, fontWeight: FontWeight.w300)),
onPressed: () async{
try{
await widget.auth.resetPassword('guanwenyan301@gmail.com');
}catch(e){
print(e);
}
},
))
],
),
));
}
}
這是我的 BaseAuth 類:
abstract class BaseAuth {
Future<String> signIn(String email, String password);
Future<String> signUp(String email, String password);
Future<FirebaseUser> getCurrentUser();
Future<void> signOut();
Future<void> resetPassword(String email);}
class Auth implements BaseAuth {
final FirebaseAuth _firebaseAuth = FirebaseAuth.instance;
Future<String> signIn(String email, String password) async {
FirebaseUser user = await _firebaseAuth.signInWithEmailAndPassword(
email: email, password: password);
if (user.isEmailVerified) {
return user.uid;
}else{
Fluttertoast.showToast(msg: 'Please verify your email');
}
}
Future<String> signUp(String email, String password) async {
FirebaseUser user = await _firebaseAuth.createUserWithEmailAndPassword(
email: email, password: password);
await user.sendEmailVerification();
return user.uid;
}
Future<FirebaseUser> getCurrentUser() async {
FirebaseUser user = await _firebaseAuth.currentUser();
return user;
}
Future<void> signOut() async {
return _firebaseAuth.signOut();
}
Future<void> resetPassword(String email) async {
return _firebaseAuth.sendPasswordResetEmail(email: email);
}}
這是我得到的錯誤:
flutter: NoSuchMethodError: The method 'resetPassword' was called on
null.
Receiver: null
Tried calling: resetPassword("guanwenyan301@gmail.com")
問題在這里
ResetPasswordPage({Key key, this.auth});
final BaseAuth auth;
當您從主小部件傳遞身份驗證時。 但在你的代碼中它不見了
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ResetPasswordPage()),
);
所以需要創建**Auth**的實例,然后傳遞給路由中的ResetPasswordPage() 。
///像這樣初始化auth對象
auth = Auth();
/// 並像這樣在 onPressed 中傳遞它
onPressed: (){
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ResetPasswordPage(auth : auth)), // first auth is the key in ResetPasswordPage and second auth is value
);
}
您在未通過身份驗證的情況下創建了 ResetPasswordPage:
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ResetPasswordPage()),
);
這意味着您的 ResetPasswordPage 下的身份驗證為空。 然后你用它來調用 resetPassword() 方法。 當然,你會得到例外。
解決方案:
...
onPressed: () {
var auth = Auth();
Navigator.push(
context,
MaterialPageRoute(builder: (context) => ResetPasswordPage(auth: auth)),
);
}
...
希望能幫助到你!
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.