flutter - 使用不包含 Navigator 的上下文请求的 Navigator 操作。(Flutter)
问题描述
嗨,我有一个场景,我根据凭据输入用户名密码,它将用户重定向到第二个屏幕。我不知道我哪里错了?但我在下面收到此错误:未处理的异常:使用不包含导航器的上下文请求导航器操作。
这是代码:
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'Animation/FadeAnimation.dart';
import 'AuthenticationService.dart';
import 'PatientList.dart';
class HomePage extends StatelessWidget {
final TextEditingController emailController = TextEditingController();
final TextEditingController passwordController = TextEditingController();
gotoPatientList(BuildContext context) {
Navigator.push(
context,
MaterialPageRoute(builder: (context) =>PatientList()),
);
}
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<AuthenticationService>(
create: (_) => AuthenticationService(FirebaseAuth.instance),
),
StreamProvider(
create: (context) =>
context.read<AuthenticationService>().authStateChanges,
),
],
child:MaterialApp(
home: SingleChildScrollView(
child:RaisedButton(
onPressed: () {
gotoPatientList(context);
context.read<AuthenticationService>().signIn(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
},
child: Container(
child: Column(
children: <Widget>[
Container(
height: 400,
decoration: BoxDecoration(
image: DecorationImage(
image: AssetImage('assets/images/loginHeader.png'),
fit: BoxFit.fill)),
child: Stack(
children: <Widget>[],
),
),
Padding(
padding: EdgeInsets.all(30.0),
child: Column(
children: <Widget>[
FadeAnimation(
1.8,
Container(
padding: EdgeInsets.all(5),
decoration: BoxDecoration(
color: Colors.white,
borderRadius: BorderRadius.circular(10),
boxShadow: [
BoxShadow(
color:
Color.fromRGBO(143, 148, 251, .2),
blurRadius: 20.0,
offset: Offset(0, 10))
]),
child: Column(
children: <Widget>[
Container(
padding: EdgeInsets.all(8.0),
decoration: BoxDecoration(
border: Border(
bottom: BorderSide(
color: Colors.grey[100]))),
child: TextField(
controller: emailController,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Email or Phone number",
hintStyle: TextStyle(
color: Colors.grey[400])),
),
),
Container(
padding: EdgeInsets.all(8.0),
child: TextField(
controller: passwordController,
obscureText: true,
decoration: InputDecoration(
border: InputBorder.none,
hintText: "Password",
hintStyle: TextStyle(
color: Colors.grey[400])),
),
)
],
),
)),
SizedBox(
height: 30,
),
FadeAnimation(
2,
Container(
height: 50,
decoration: BoxDecoration(
borderRadius: BorderRadius.circular(10),
gradient: LinearGradient(colors: [
Color.fromRGBO(214, 0, 27, 1),
Color.fromRGBO(214, 0, 27, 1),
])),
child: Center(
child: Text(
"Login",
style: TextStyle(
color: Colors.white,
fontWeight: FontWeight.bold),
),
),
)),
SizedBox(
height: 70,
),
FadeAnimation(
1.5,
Text(
"Forgot Password?",
style: TextStyle(
color: Color.fromRGBO(214, 0, 27, 1)),
)),
],
),
)
],
),
),
),
),
),
);
}
}
class AuthenticationWrapper extends StatelessWidget {
@override
Widget build(BuildContext context) {
final firebaseUser = context.watch<User>();
if (firebaseUser != null) {
return HomePage();
}
return PatientList();
}
}
解决方案
您可以将按钮包装在 builder 中以获取应用程序的上下文:
Builder(
builder: (context) => RaisedButton(
onPressed: () {
gotoPatientList(context);
context.read<AuthenticationService>().signIn(
email: emailController.text.trim(),
password: passwordController.text.trim(),
);
},
...
),
);
推荐阅读
- mysql - 仅将 MySQL uniq 行增加一个请求
- visual-studio-code - 如何打开这个“控制台”在 VS Code 中安装包?
- security - 如果使用 SSL,是否有必要对 POST 的数据进行哈希处理?
- php - 如何在 PHP 中使用 simple_html_dom 查找视频源 url
- powershell - PowerShell 枚举评估
- visual-studio-code - 如何在 vscode 中删除现有的 jupyter 服务器
- android - 改造响应返回“NumberFormatexception:空字符串”
- javascript - 反应 useState 不是一个函数
- api - 是否可以使用 Word Javascript API 刷新文档的字段?
- css - 从 Angular 文件夹中删除未使用的 component.scss 文件