I just started learning flutter. I was creating a guide app for a game. And I encountered this error.
Another exception was thrown: Could not find a generator for route RouteSettings("/bo3morgmap", null) in the _WidgetsAppState.
main.dart
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
import 'package:flutter/material.dart';
import 'pages/blackops3/blackops3.dart';
import 'pages/blackops3/morgmap.dart';
void main() {
runApp(MainApp(),);
}
class MainApp extends StatelessWidget {
const MainApp({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(title: const Text("Zombies Assistants"), backgroundColor: Color.fromARGB(255, 107, 21, 204), centerTitle: true),
drawer: Builder(
builder: (context) {
return Drawer(
child: Column(
children: [
DrawerHeader(child: Icon(Icons.games, size: 64,)),
ListTile(
leading: Icon(Icons.home),
title: Text("Main Menu"),
onTap: () {
Navigator.popAndPushNamed(context, '/main');
},
),
ListTile(
leading: Icon(Icons.gamepad),
title: Text("Black Ops 3"),
onTap: () {
Navigator.popAndPushNamed(context, '/blackops3');
},
),
],
),
);
}
),
),
routes: {
'/blackops3': (context) => blackops3(),
'/main': (context) => MainApp(),
'/bo3morgmap': (context) => morgmap(),
},
theme: ThemeData(
useMaterial3: true,
),
);
}
}
blackops3.dart
import 'package:flutter/material.dart';
// ignore: unnecessary_import
import 'package:flutter/widgets.dart';
// ignore: camel_case_types
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
// ignore: camel_case_types
class blackops3 extends StatelessWidget {
const blackops3({super.key});
@override
Widget build(BuildContext context) {
return MaterialApp(
home: Scaffold(
appBar: AppBar(
leading: IconButton(icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.of(context).pop();}
),
title: const Text("Black Ops 3"), backgroundColor: Color.fromARGB(255, 107, 21, 204), centerTitle: true,
),
body: Builder(
builder: (context) {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: ElevatedButton(
onPressed: () {Navigator.pushReplacementNamed(context, '/bo3morgmap');},
child: Text("Morg City Map"),
),
),
),
Center(
child: ElevatedButton(onPressed: () {Navigator.pushReplacementNamed(context, '/bo3morgmap');},
child: Text("Train Signs Helper"),
),
),
],
);
}
),
),
);
}
}
morgmap.dart
import 'package:flutter/material.dart';
// ignore: camel_case_types
// ignore_for_file: prefer_const_constructors, prefer_const_literals_to_create_immutables
// ignore: camel_case_types
class morgmap extends StatelessWidget {
const morgmap({super.key});
@override
Widget build(BuildContext context){
return Scaffold(
appBar: AppBar(
leading: IconButton(icon: Icon(Icons.arrow_back),
onPressed: () {
Navigator.of(context).pop();}
),
title: const Text("Black Ops 3"), backgroundColor: Color.fromARGB(255, 107, 21, 204), centerTitle: true,
),
body: Builder(
builder: (context) {
return Column(
children: [
Text("Im working")
],
);
}
),
);
}
}
I also think I'm doing things wrong.
.....
body: Builder(
builder: (context) {
return Column(
children: [
Padding(
padding: const EdgeInsets.all(8.0),
child: Center(
child: ElevatedButton(
onPressed: () {Navigator.pushReplacementNamed(context, '/bo3morgmap');},
child: Text("Morg City Map"),
),
),
),
Center(
child: ElevatedButton(onPressed: () {Navigator.pushReplacementNamed(context, '/bo3morgmap');},
child: Text("Train Signs Helper"),
),
),
],
);
}
),
.....
Am I supposed to put the 2 elevated buttons in the same child? I don't think that's possible.
I want the navigator to navigate through morgmap. I tried adding () => (I saw it on another post) but it still didn't work. And I don't think that is the problem. Even if I add it it still doesn't navigates through.
Your issue likely is due to instantiating MaterialApp
twice.
main.dart
(encouraged, keep this one),blackops3.dart
(remove this one).See MaterialApp
as the root component/entry point of your application, and you should only need to instantiate it once per app. Read more about MaterialApp here.
So: remove the MaterialApp
inside blackops3.dart
, like so:
class blackops3 extends StatelessWidget {
const blackops3({super.key});
@override
Widget build(BuildContext context) {
return Scaffold( ... ); // <-- Here we've removed the wrapping `MaterialApp`
}
}
When you instantiate MaterialApp
inside blackops3.dart
you create a new app scope, in which your routes (which you declared in the top-most MaterialApp
) no longer exist.
Therefore your call to
Navigator.pushReplacementNamed(context, '/bo3morgmap');
Throws an exception, since there is no defined route for the path you've provided.