【发布时间】:2022-01-25 18:14:10
【问题描述】:
我收到一个异常:FlutterError(使用不包含 Navigator 的上下文请求的导航器操作。用于从 Navigator 推送或弹出路由的上下文必须是作为 Navigator 小部件后代的小部件的上下文。 )
这是第一页的重要代码(跳过了一些与...无关的代码):
import 'package:google_fonts/google_fonts.dart';
import 'register.dart';
void main() async {
...
runApp(const MyApp());
}
class MyApp extends StatefulWidget {
const MyApp({Key? key}) : super(key: key);
@override
State<MyApp> createState() => _MyAppState();
}
class _MyAppState extends State<MyApp> {
...
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: false,
theme: ThemeData(
textTheme: GoogleFonts.interTextTheme(
Theme.of(context).textTheme,
)),
home: Scaffold(
body: Container(
width: double.infinity,
...
Align(
alignment: Alignment.centerRight,
child: TextButton(
onPressed: () {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => SecondRoute()));
},
child: Text(
"Forgot Password?",
style: GoogleFonts.inter(
fontWeight: FontWeight.w600,
),
),
),
),
...
),
));
} }
这是第二页:
import 'package:flutter/material.dart';
class SecondRoute extends Navigator {
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: AppBar(
title: Text("Second Route"),
),
);
}
}
没有语法错误,只有在代码运行时抛出异常。我已经尝试过寻找解决方案,但根据我的理解,将材料应用程序放入 runApp() 方法的另一种方法不适用于我使用文本主题和使用上下文的方式。
如果我需要提供更多代码或上下文,请告诉我。
任何帮助将不胜感激!
【问题讨论】: