I've decided to give flutter a try and stumbled upon the following problem:
In my app I would like to have a button in the AppBar that would take the user to another screen. However whenever I attempt to call Navigator in any of the IconButtons' onPressed handlers, I get the following error:
Navigator operation requested with a context that does not include a Navigator. The context used to push or pop routes from the Navigator must be that of a widget that is a descendant of a Navigator widget.
Here's my main.dart (minus the imports):
void main() => runApp(new MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
var service = new ProjectService();
return new MaterialApp(
theme: new ThemeData(
primaryColor: Colors.deepOrange,
),
home: new Scaffold(
appBar: new AppBar(
title: new Text("My App Title"),
actions:
<Widget>[
new IconButton(
icon: new Icon(Icons.add),
onPressed: () =>
Navigator.of(context).pushNamed("/projects/add")) //<-- Here's where I get the exception
],
elevation: 0.0),
body: new Center(child: new ProjectList(projects: service.toList())), //<-- Exactly the same code runs fine here
drawer: new AppDrawer(),
),
routes: <String, WidgetBuilder>{
'/projects/add': (BuildContext context) => new ProjectAddScreen(),
'/projects/test': (BuildContext context) => new ProjectAddScreen(),
'/settings': (BuildContext context) => new AppSettingsScreen(),
'/about': (BuildContext context) => new AboutScreen()
});
}
}
This is the only place where the app instance is constructed and AppBar
is the standard AppBar
that comes bundled with the flutter/material package.
My flutter doctor
output if that's of any relevance
[√] Flutter (Channel master, v0.2.5-pre.41, on Microsoft Windows [Version 10.0.16299.309], locale en-US)
[!] Android toolchain - develop for Android devices (Android SDK 26.0.2)
X Android license status unknown. // I use the SDK manager from Visual Studio
[!] Android Studio (version 3.0)
X Unable to determine bundled Java version. //I use the SDK manager from Visual Studio so I have no need in Android Studio
[√] VS Code, 64-bit edition (version 1.21.1)
[√] Connected devices (1 available)