ios – Android Studio Navigator operation requested with a context that does not include a Navigator

[ad_1]

I want to go to Second Page with Navigator.push method. So I am using ElevatedButton Widget for this purpose. I’m also using Statefull Widget. My source code like in below:

import 'dart:io' show Platform;
import 'package:flutter/material.dart';
import 'package:qr_code_scanner/qr_code_scanner.dart';
import 'ScannerPage.dart';

void main() {
  runApp(const QrCodeMainWindow());
}

class QrCodeMainWindow extends StatefulWidget {
  const QrCodeMainWindow({Key? key}) : super(key: key);

  @override
  State<QrCodeMainWindow> createState() => _QrCodeMainWindowState();
}

class _QrCodeMainWindowState extends State<QrCodeMainWindow> {
  final String _data = "";
  QRViewController? controller;
  Barcode? result;
  final GlobalKey qrKey = GlobalKey(debugLabel: 'QR');

  @override
  Widget build(BuildContext context) {
    final ButtonStyle style = ElevatedButton.styleFrom(
      textStyle: const TextStyle(fontSize: 20),
    );
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('QR Code Scanner App'),
          backgroundColor: Colors.blueAccent,
        ),
        body: Column(
          children: [
            ElevatedButton(
                child: Text('Scan'),
                onPressed: () {
                  Navigator.push(
                    context,
                    MaterialPageRoute(builder: (context) => ScannerPage()),
                  );
                }),
            Text(_data)
          ],
        ),
      ),
    );
  }

When I run this App succesfuly openning on simulator (Iphone 13). But when I press the button I get below error.
How Can I solve this error ?

======== 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      _QrCodeMainWindowState.build.<anonymous closure> (package:qr_code_scanner_example/main.dart:51:29)
#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.handlePrimaryPointer (package:flutter/src/gestures/tap.dart:230:7)
#9      PrimaryPointerGestureRecognizer.handleEvent (package:flutter/src/gestures/recognizer.dart:563:9)
#10     PointerRouter._dispatch (package:flutter/src/gestures/pointer_router.dart:94:12)
#11     PointerRouter._dispatchEventToRoutes.<anonymous closure> (package:flutter/src/gestures/pointer_router.dart:139:9)
#12     _LinkedHashMapMixin.forEach (dart:collection-patch/compact_hash.dart:539:8)
#13     PointerRouter._dispatchEventToRoutes (package:flutter/src/gestures/pointer_router.dart:137:18)
#14     PointerRouter.route (package:flutter/src/gestures/pointer_router.dart:123:7)
#15     GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:439:19)
#16     GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:419:22)
#17     RendererBinding.dispatchEvent (package:flutter/src/rendering/binding.dart:322:11)
#18     GestureBinding._handlePointerEventImmediately (package:flutter/src/gestures/binding.dart:374:7)
#19     GestureBinding.handlePointerEvent (package:flutter/src/gestures/binding.dart:338:5)
#20     GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:296:7)
#21     GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:279:7)
#25     _invoke1 (dart:ui/hooks.dart:170:10)
#26     PlatformDispatcher._dispatchPointerDataPacket (dart:ui/platform_dispatcher.dart:331:7)
#27     _dispatchPointerDataPacket (dart:ui/hooks.dart:94:31)
(elided 3 frames from dart:async)
Handler: "onTap"
Recognizer: TapGestureRecognizer#ba4ae
  debugOwner: GestureDetector
  state: possible
  won arena
  finalPosition: Offset(46.3, 135.7)
  finalLocalPosition: Offset(46.3, 26.7)
  button: 1
  sent tap down
      

[ad_2]

Source link

Leave a Reply

Your email address will not be published.