我尝试开发Flutter应用程序。在此应用程序中,我使用带有用户图标的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"), ), ); } }
仍然无法解决该错误,但是我仍然遵循一些文档和堆栈溢出问题。
Stackoverflow question and answer
我尝试开发Flutter应用程序。在此应用程序中,我使用带有用户图标的Appbar浏览另一个页面。现在,我单击了显示错误的图标(人形图标)。 。它不正确...
尝试在构建器中使用上下文
这里的问题是在父上下文中不存在导航器。您正在使用MyApp的上下文,该上下文不在导航器下。
问题是上述问题。用我自己的话:您从中调用“导航器”的上下文不包含“导航器”。我猜问题是在MaterialApp完成构建方法并获得Navigator之类的代码之前,您在代码中调用了Scaffold。如果将MaterialApp和Scaffold分开(如下所示),则可以解决问题。
IconButton(
onPressed: () {
Navigator.of(context).push(new MaterialPageRoute(builder: (context)=> AdminAuth));
},
)
[MateriaApp上下文在库中存在一些问题。您的代码将无法正常工作。创建一个不同的MaterialApp,然后在MaterialApp的home: