参数类型 'Context' 无法分配给参数类型 'BuildContext'
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"
依赖项:
扑:
SDK: 颤动
curved_navigation_bar: ^0.3.2
欧克吐司:
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);
}
}
}
我正在尝试使用 flutter 和 firebase 创建一个登录页面,导航器在所有文件中都能正常工作,但是当进入此页面时,它在导航器的上下文中显示错误
错误是
'无法将参数类型 'Context' 分配给参数类型 'BuildContext'。
以下是我的 flutter 版本的详细信息:
版本:1.0.0+1
环境: SDK: ">=2.1.0 <3.0.0"
依赖项: 扑: SDK: 颤动 curved_navigation_bar: ^0.3.2 欧克吐司: 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);
}
}
}