当我尝试导航到下一个屏幕时,抛出该错误。我之前已经面对过,在树中添加新的上下文通常是可行的。这次不是。我该怎么办?
Error:
E/flutter (22865): [ERROR:flutter/lib/ui/ui_dart_state.cc(166)]
Unhandled Exception: NoSuchMethodError: The method 'findAncestorStateOfType' was called on null.
E/flutter (22865): Receiver: null
E/flutter (22865): Tried calling: findAncestorStateOfType()
class _LoginMainState extends State<LoginMain> {
@override
Widget build(BuildContext context) {
return MaterialApp(
home: SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Sorted.'),
backgroundColor: Color(0xff0A3D62),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
const Color(0xFF5761B2),
const Color(0xFF1FC588),
])),
),
),
drawer: null,
body: Container(
padding: EdgeInsets.only(top: 30),
width: 360,
height: 600,
color: const Color(0xFF273748),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: 300,
height: 400,
child: Container(
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(20.0),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.white, Colors.grey[400]]),
),
child: SizedBox(
width: 50,
height: 50,
child: Container(
child: MaterialButton(
color: Colors.green,
splashColor: Colors.teal,
textColor: Colors.white,
child: new Icon(Icons.arrow_forward),
onPressed: () async {
bool res = await AuthProvider().loginWithGoogle();
if (res) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SortedMain()));
} else
print("Login failed");
谢谢!
答案 0 :(得分:0)
问题是因为MaterialApp
上方没有上下文。 Navigator
取决于MaterialApp
的上下文,并且由于唯一可用的上下文位于应用程序上方,因此会引发错误。
您提到您以前曾遇到过此问题,并通过添加其他上下文对其进行了修复。但是您没有指定添加的位置。
要解决此问题,只需使用Builder
将内容包装在MaterialApp下,或将其移动到单独的无状态或有状态小部件。
class _LoginMainState extends State<LoginMain> {
@override
Widget build(BuildContext context) {
return MaterialApp(
// Added this
home: Builder(
builder: (context) => SafeArea(
child: Scaffold(
appBar: AppBar(
title: Text('Sorted.'),
backgroundColor: Color(0xff0A3D62),
flexibleSpace: Container(
decoration: BoxDecoration(
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: <Color>[
const Color(0xFF5761B2),
const Color(0xFF1FC588),
])),
),
),
drawer: null,
body: Container(
padding: EdgeInsets.only(top: 30),
width: 360,
height: 600,
color: const Color(0xFF273748),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
children: [
SizedBox(
width: 300,
height: 400,
child: Container(
padding: EdgeInsets.all(15),
decoration: BoxDecoration(
shape: BoxShape.rectangle,
borderRadius: BorderRadius.circular(20.0),
gradient: LinearGradient(
begin: Alignment.topLeft,
end: Alignment.bottomRight,
colors: [Colors.white, Colors.grey[400]]),
),
child: SizedBox(
width: 50,
height: 50,
child: Container(
child: MaterialButton(
color: Colors.green,
splashColor: Colors.teal,
textColor: Colors.white,
child: new Icon(Icons.arrow_forward),
onPressed: () async {
bool res = await AuthProvider().loginWithGoogle();
if (res) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SortedMain()));
} else
print("Login failed");
如果这行不通,只需将页面移至其他小部件(无状态或有状态)。