Issue
I am currently having issues navigating from one page to another on my flutter application. I am really new to Flutter, as this is my first application, and this site has helped me before so I'm hoping it will help me again.
The code below in the "//Drawer List" section is having issues with the "Navigator.push" within "onTap" which I want to implement for multiple ListTiles. The code runs great in my Android Emulator, but when I click the ListTile in the drawer I get this in the terminal:
══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
The following assertion was thrown while handling a gesture:
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.
When the exception was thrown, this was the stack:
#0 Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:2553:9)
#1 Navigator.of (package:flutter/src/widgets/navigator.dart:2560:6)
#2 Navigator.push (package:flutter/src/widgets/navigator.dart:2016:22)
#3 MyApp.build.<anonymous closure> (package:bit/main.dart:58:27)
#4 _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:989:21)
#5 GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:198:24)
#6 TapGestureRecognizer.handleTapUp (package:flutter/src/gestures/tap.dart:608:11)
#7 BaseTapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:296:5)
#8 BaseTapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:267:7)
#9 GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:157:27)
#10 GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:443:20)
#11 GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
#12 RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:322:11)
#13 GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
#14 GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
#15 GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
#16 GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
#20 _invoke1 (dart:ui/hooks.dart:170:10)
#21 PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331:7)
#22 _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer:
TapGestureRecognizer#d5f04
════════════════════════════════════════════════════════════════════════════════════════════════════
After clicking it a second time a second error comes up and then repeats.
Another exception was thrown: Navigator operation requested with a context that does not include a
Navigator.
I tried messing with a few solutions I found online, but all of them seemed to give me the same error so I was hoping someone could help me fix/find what I am missing in the below code. Thank you.
import 'package:flutter/material.dart';
void main() {
runApp(const MaterialApp(home: MyApp(),
));
}
class MyApp extends StatelessWidget {
const MyApp({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
var scaffold = Scaffold(
appBar: AppBar(
title: const Text('Test App'),
),
// Body Home Page Beginning
body: SingleChildScrollView(
child: Container(
child: const Center(
child: Text('Hello'),
))),
// Body Home Page End
drawer: Drawer(
// Drawer Beginning
child: ListView(
children: [
// Drawer Header
DrawerHeader(
decoration: const BoxDecoration(
color: Colors.blue,
),
child: Stack(
children: const [
Align(
alignment: Alignment.centerLeft,
child: CircleAvatar(
backgroundColor: Colors.white,
radius: 50.0,
)),
Align(
alignment: Alignment.centerRight,
child: Text(
'Username',
style: TextStyle(
color: Colors.white,
fontSize: 20.0,
),
))
],
),
),
// Drawer List
ListTile(
title: const Text('Profile'),
subtitle: const Text('Account Information'),
onTap: () {
Navigator.push(context, MaterialPageRoute(builder: (context) => const About()),
);
},
trailing: const Icon(Icons.arrow_forward_ios_rounded),
),
const ListTile(
title: Text('Education'),
subtitle: Text('Homeschool Education Material & Quizzes'),
trailing: Icon(Icons.arrow_forward_ios_rounded),
),
const ListTile(),
],
),
),
// Drawer End
);
return MaterialApp(
title: 'Test App',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: scaffold,
);
}
}
class About extends StatelessWidget {
const About({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
);
}
}
class SinTracker extends StatelessWidget {
const SinTracker({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(),
);
}
}
All code is stored in "main.dart" file.
Solution
VS Code and my emulator just needed a reboot/restart. Someone checked it in the comments and I am simply providing an anwser to get it off the questions list.
Answered By - Jared
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.