0

我尝试开发颤振应用程序。在这个应用程序中,我使用带有用户图标的 Appbar 来导航另一个页面。

带有 Appbar 和人形图标的模拟器

现在我点击了那个图标(人形图标)它显示错误。

调试器中的错误.

通过互联网,它没有适当的文档。我找不到答案。我的源代码是

class MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new Scaffold(
            appBar: new AppBar(
              title: new Text("Web Issue finder"),
              actions: <Widget>[
                new IconButton(
                  icon: Icon(Icons.person),
//                  tooltip: "Admin",
                  onPressed: (){
                    Navigator.push(
                      context,
                      MaterialPageRoute(builder: (_) => AdminAuth()),
                    );
                  }
                )
              ],
            ),
            body: new FutureBuilder(
                future: loadStates(),
                builder: (context, snapshot) {
                  if (snapshot.hasData) {
                    return new ListView.builder(
                      itemBuilder: (context, index) {
                        if (index >= snapshot?.data?.length ?? 0) return null;

                        return new ListTile(
                          title: new Text("${snapshot.data[index]}"),
                          onTap: () {
                            debugPrint("${snapshot.data[index]} clicked");
                            Navigator.push(
                              context,
                              MaterialPageRoute(
                                builder: (context) =>
                                    IssueAddScreen(state: snapshot.data[index]),
                              ),
                            );
                          },
                        );
                      },
                    );
                  } else {
                    return new Center(child: new CircularProgressIndicator());
                  }
                })));
  }

这是导航类

import 'package:flutter/material.dart';


class AdminAuth extends StatelessWidget{

//  final String state;

//  IssueAddScreen({Key key, @required this.state}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return MaterialApp(
      debugShowCheckedModeBanner: false,
      title: "iWallet",
      home: Scaffold(
        appBar: AppBar(title: Text("admin auth"),),
        body: Text("cvgbh"),
      ),

    );
  }

}

我仍然无法修复该错误,我遵循了一些文档和堆栈溢出问题。

颤振文档

Github问答

Stackoverflow 问答

4

5 回答 5

0

这里的问题是导航器不存在于父上下文中。您正在使用不在导航器下的 MyApp 的上下文。

MyApp    <------ context
  --> MaterialApp
   (--> Navigator built within MaterialApp)
      --> Scaffold
        --> App Bar
          --> ...

解决这个问题 - 定义包含MaterialApp然后传入MyApp()的新home:MaterialApp

对于AdminAuth.

class MyAppHome extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyApp(),
    );
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}


class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: new AppBar(
          title: new Text("Web Issue finder"),
          actions: <Widget>[
            new IconButton(
                icon: Icon(Icons.person),
//                  tooltip: "Admin",
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (_) => AdminAuth()),
                  );
                })
          ],
        ),
        body: new FutureBuilder(
            future: loadStates(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return new ListView.builder(
                  itemBuilder: (context, index) {
                    if (index >= snapshot?.data?.length ?? 0) return null;

                    return new ListTile(
                      title: new Text("${snapshot.data[index]}"),
                      onTap: () {
                        debugPrint("${snapshot.data[index]} clicked");
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) =>
                                IssueAddScreen(state: snapshot.data[index]),
                          ),
                        );
                      },
                    );
                  },
                );
              } else {
                return new Center(child: new CircularProgressIndicator());
              }
            }));
  }
}
于 2019-02-25T11:43:43.673 回答
0

库中的 MateriaApp 上下文存在一些问题。您的代码将不起作用。创建一个不同的 MaterialApp,然后在home 中使用您的小部件: MaterialApp 的属性。

例如:

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(
        primarySwatch: Colors.lightBlue,
      ),
      home: MyAppState(), //This is your MyAppState
    );
  }
}

现在您可以在 MyAppState 中删除 MaterialApp 小部件,仅保留Scaffold小部件

于 2019-10-28T10:55:11.177 回答
0

尝试在构建器中使用上下文

Navigator.push(context,MaterialPageRoute(builder: (BuildContext context){return AdminAuth();
});
于 2019-02-24T18:36:37.073 回答
0

问题是上面解释的问题。用我自己的话来说:您调用“导航器”的上下文不包含“导航器”。我想问题是在您的代码中,您在 MaterialApp 完成构建方法并获得 Navigator 或类似的东西之前调用了 Scaffold。如果您将 MaterialApp 和 Scaffold 分开(如下所示),您就可以解决问题。

void main() => runApp(MaterialApp(
    home: MyApp())


class MyAppState extends State<MyApp> {
@override
Widget build(BuildContext context) {
return new Scaffold(
        appBar: new AppBar(
          title: new Text("Web Issue finder"),
          actions: <Widget>[
            new IconButton(
              icon: Icon(Icons.person),
            tooltip: "Admin",
              onPressed: (){
                Navigator.push(
                  context,
                  MaterialPageRoute(builder: (_) => AdminAuth()),
                );
              }
            )
          ],
        ),
        body: new FutureBuilder(
            future: loadStates(),
            builder: (context, snapshot) {
              if (snapshot.hasData) {
                return new ListView.builder(
                  itemBuilder: (context, index) {
                    if (index >= snapshot?.data?.length ?? 0) return null;

                    return new ListTile(
                      title: new Text("${snapshot.data[index]}"),
                      onTap: () {
                        debugPrint("${snapshot.data[index]} clicked");
                        Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (context) =>
                                IssueAddScreen(state: snapshot.data[index]),
                          ),
                        );
                      },
                    );
                  },
                );
              } else {
                return new Center(child: new CircularProgressIndicator());
              }
            })));
于 2019-10-24T14:45:17.910 回答
0
IconButton(onPressed: () {
           Navigator.of(context).push(new MaterialPageRoute(builder: (context)=> AdminAuth));
        },)
于 2019-10-28T10:00:01.363 回答