【问题标题】:Exception when trying to use navigator尝试使用导航器时出现异常
【发布时间】: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() 方法的另一种方法不适用于我使用文本主题和使用上下文的方式。

如果我需要提供更多代码或上下文,请告诉我。

任何帮助将不胜感激!

【问题讨论】:

    标签: android flutter dart


    【解决方案1】:

    尝试改变:

    class SecondRoute extends Navigator {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: AppBar(
            title: Text("Second Route"),
          ),
        );
      }
    }
    

    到:

    class SecondRoute extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return new Scaffold(
          appBar: AppBar(
            title: Text("Second Route"),
          ),
        );
      }
    }
    

    编辑,还将脚手架从_MyAppState 移动到它自己的小部件中。

    class FirstRoute extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          body: SizedBox(
            width: double.infinity,
            child: Align(
              alignment: Alignment.centerRight,
              child: TextButton(
                onPressed: () {
                  print('hello');
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => SecondRoute()),
                  );
                },
                child: Text(
                  "Forgot Password?",
                  style: GoogleFonts.inter(
                    fontWeight: FontWeight.w600,
                  ),
                ),
              ),
            ),
          ),
        );
      }
    }
    

    然后像这样在_MyAppState中使用:

    class _MyAppState extends State<MyApp> {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          theme: ThemeData(
              textTheme: GoogleFonts.interTextTheme(
            Theme.of(context).textTheme,
          )),
          home: FirstRoute(),
        );
      }
    }
    

    【讨论】:

    • 这应该工作
    • 我原来是这样的,改成navigator来解决这个问题,但是还是不行。仍然给我同样的例外。
    • 查看我的更新答案。
    • 无论如何我可以将它与有状态小部件一起保存,因为我的其余代码依赖于它是一个有状态小部件。
    • FirstRoute 如果您愿意,可以改为有状态小部件。该问题是由BuildContext 对象引起的。在您的原始代码中,BuildContext 是在MaterialApp 之外定义的,但是将路由分离到它自己的小部件中解决了这个问题。另一种方法是使用Builder 小部件并使用Builder 小部件中的BuildContext。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-10
    • 2017-08-13
    • 1970-01-01
    • 2016-12-28
    • 1970-01-01
    相关资源
    最近更新 更多