![](/img/trans.png)
[英]The Argument type 'Context' can't be assigned to the parameter type 'BuildContext' - Flutter
[英]The argument type 'Context' can't be assigned to the parameter type 'BuildContext'
我正在嘗試創建一個帶有 flutter 和 firebase 的登錄頁面,導航器在所有文件中都正常工作,但是當進入此頁面時,它在導航器的上下文中顯示錯誤,錯誤是
'不能將參數類型 'Context' 分配給參數類型 'BuildContext'。 以下是我的flutter版本的詳細信息:版本:1.0.0+1
環境:sdk:">=2.1.0 <3.0.0"
依賴項:flutter:sdk:flutter curve_navigation_bar:^0.3.2 oktoast:firebase_auth:^0.5.20 請幫我解決這個問題
import 'package:path/path.dart';
import 'package:codej/effects.dart';
import 'package:flutter/material.dart';
import 'package:oktoast/oktoast.dart';
import 'package:firebase_auth/firebase_auth.dart';
class Auth extends StatefulWidget {
@override
_AuthState createState() => new _AuthState();
}
class _AuthState extends State<Auth>{
String _email, _password;
final GlobalKey<FormState> formkey = GlobalKey<FormState>();
Widget build(BuildContext context) {
// TODO: implement build
return Scaffold(appBar: AppBar(
title: Text("Authentication"),
),
// TODO: implement build
body: Form(
key: formkey,
child: Column(
children: <Widget>[
TextFormField(
validator: (input){
if(input.isEmpty){
showToastWidget(Text("please enter your user name"));
}
},
onSaved: (input) => _email = input,
decoration: InputDecoration(
labelText: 'Email'
),
),
TextFormField(
validator: (input){
if(input.length < 6){
showToastWidget(Text("please enter your password atleast 6 characters"));
}
},
onSaved: (input) => _password = input,
decoration: InputDecoration(
labelText: 'Password'
),
obscureText: true,
),
RaisedButton(onPressed: (){
},
child: Text("Sign in"),)
],
)),
);
}
void signinAuth() async{
final formState = formkey.currentState;
if(formState.validate()){
formState.save();
try{
FirebaseUser user = await FirebaseAuth.instance.signInWithEmailAndPassword(email: _email, password: _password);
Navigator.push(context, MaterialPageRoute(builder: (context) => BottomNav())); //BottomNav loacted in other file
}catch(e){
print(e.message);
}
}
}
}
這在我的電腦上工作,所以它是版本問題。 嘗試更新您的 flutter 和 dart sdk,如果這不是您的解決方案,只需像這樣將 BuildContext 傳遞給您的 signinAuth
void signinAuth(BuildContext context) async {
final formState = formkey.currentState;
if (formState.validate()) {
formState.save();
try {
FirebaseUser user = await FirebaseAuth.instance
.signInWithEmailAndPassword(email: _email, password: _password);
Navigator.push(
context,
MaterialPageRoute(
builder: (context) =>
BottomNav())); //BottomNav loacted in other file
} catch (e) {
print(e.message);
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.