我正在尝试使用flutter和firebase创建一个登录页面,导航器在所有文件中均正常工作,但是进入此页面时,它在导航器上下文中显示错误
错误是
'参数类型'Context'不能分配给参数类型'BuildContext'。
以下是我的flutter版本的详细信息:
版本:1.0.0 + 1
环境: sdk:“> = 2.1.0 <3.0.0”
依赖关系: 扑: sdk:颤动 弯曲的导航栏:^ 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);
}
}
}
}
答案 0 :(得分:0)
这在我的PC上有效,因此是版本问题。尝试更新您的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);
}
}
}