6

I am trying to show a non dismissable dialog after verifying the textfields in a form but it keeps printing:

03-22 12:34:46.373 8974-9001/com.mywebsite I/flutter: ══╡ EXCEPTION CAUGHT BY GESTURE ╞═══════════════════════════════════════════════════════════════════
03-22 12:34:46.397 8974-9001/com.mywebsite I/flutter: The following assertion was thrown while handling a gesture:
03-22 12:34:46.397 8974-9001/com.mywebsite I/flutter: Navigator operation requested with a context that does not include a Navigator.
03-22 12:34:46.397 8974-9001/com.mywebsite I/flutter: The context used to push or pop routes from the Navigator must be that of a widget that is a
03-22 12:34:46.397 8974-9001/com.mywebsite I/flutter: descendant of a Navigator widget.
03-22 12:34:46.404 8974-9001/com.mywebsite I/flutter: When the exception was thrown, this was the stack:
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #0      Navigator.of.<anonymous closure> (package:flutter/src/widgets/navigator.dart:725:9)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #1      Navigator.of (package:flutter/src/widgets/navigator.dart:731:6)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #2      showDialog (package:flutter/src/material/dialog.dart:486:20)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #3      SignupBodyState._showProgressDialog (package:truck_am_easy/signup.dart:310:5)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #4      SignupBodyState._verifyInputs (package:truck_am_easy/signup.dart:332:9)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #5      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:478:14)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #6      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:530:30)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #7      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #8      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:161:9)
03-22 12:34:46.419 8974-9001/com.mywebsite I/flutter: #9      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:123:7)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #10     GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #11     BindingBase&GestureBinding.handleEvent (package:flutter/src/gestures/binding.dart:147:20)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #12     BindingBase&GestureBinding.dispatchEvent (package:flutter/src/gestures/binding.dart:121:22)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #13     BindingBase&GestureBinding._handlePointerEvent (package:flutter/src/gestures/binding.dart:101:7)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #14     BindingBase&GestureBinding._flushPointerEventQueue (package:flutter/src/gestures/binding.dart:64:7)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #15     BindingBase&GestureBinding._handlePointerDataPacket (package:flutter/src/gestures/binding.dart:48:7)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #16     _invoke1 (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:134)
03-22 12:34:46.420 8974-9001/com.mywebsite I/flutter: #17     _dispatchPointerDataPacket (file:///b/build/slave/Linux_Engine/build/src/flutter/lib/ui/hooks.dart:91)
03-22 12:34:46.425 8974-9001/com.mywebsite I/flutter: Handler: onTap
03-22 12:34:46.425 8974-9001/com.mywebsite I/flutter: Recognizer:
03-22 12:34:46.425 8974-9001/com.mywebsite I/flutter:   TapGestureRecognizer#01b7d(debugOwner: GestureDetector, state: ready, won arena, finalPosition:
03-22 12:34:46.425 8974-9001/com.mywebsite I/flutter:   Offset(187.0, 542.0), sent tap down)
03-22 12:34:46.425 8974-9001/com.mywebsite I/flutter: ════════════════════════════════════════════════════════════════════════════════════════════════════

but refused to show the dialog.

Please what am I doing wrong?

Part of my Code

@override
  Widget build(BuildContext context) {
    return new MediaQuery(
        data: new MediaQueryData(),
        child: new MaterialApp(
          home: _buildHomeUI(),
        ));
  }

  Widget _buildHomeUI() {
    return new Scaffold(
        key: _scaffoldKey,
        body: new SafeArea(
          top: false,
          bottom: false,
          child: new Container(
              decoration: new BoxDecoration(color: MyColors.colorPrimary),
              child: new ListView(
                children: <Widget>[
//                  I removed the text fields.
                  new Container(
                    decoration:
                        new BoxDecoration(color: MyColors.colorBackground),
                    padding: const EdgeInsets.only(
                        top: 10.0, left: 15.0, right: 15.0),
                    child: new Form(
                      key: _formKey1,
                      autovalidate: _autoValidate,
                      child: new Column(
                        children: <Widget>[
                          new Container(
                            margin: const EdgeInsets.symmetric(
                                horizontal: 50.0, vertical: 25.0),
                            padding:
                                const EdgeInsets.symmetric(horizontal: 30.0),
                            decoration: new BoxDecoration(
                                borderRadius: new BorderRadius.circular(30.0),
                                color: MyColors.colorAccent),
                            child: new Container(
                              margin:
                                  const EdgeInsets.symmetric(horizontal: 10.0),
                              padding:
                                  const EdgeInsets.symmetric(vertical: 5.0),
                              child: new FlatButton(
                                  onPressed: _verifyInputs,
                                  child: const Text(Strings.signUp),
                                  highlightColor: MyColors.colorAccentDark,
                                  textColor: Colors.white),
                            ),
                          ),

                        ],
                      ),
                    ),
                  )
                ],
              )),
        ));
  }

void _showProgressDialog() {
    showDialog(
        context: context,
        barrierDismissible: false,
        child: new Dialog(
          child: new Row(
            mainAxisSize: MainAxisSize.min,
            children: <Widget>[
              new CircularProgressIndicator(),
              new Text(
                "Creating your details...",
                style: const TextStyle(fontFamily: Strings.customFont),
              )
            ],
          ),
        ));
  }

  void _verifyInputs() {
    final form = _formKey1.currentState;
    if (form.validate()) {
      if (checkBoxValue) {
        form.save();
        _showProgressDialog();
        _parseResponse();
      } else {
        final snackBar = new SnackBar(
          content: new Text(
            "Please accept the terms and conditions",
            style: const TextStyle(fontFamily: Strings.customFont),
          ),
        );
        _scaffoldKey.currentState.showSnackBar(snackBar);
      }
    } else {
      _autoValidate = true;
    }
  }

  void _parseResponse() async {
    Map response = await registerUser();
    Navigator.pop(context);
    print(response);
  }
Community
  • 1
  • 1
X09
  • 3,827
  • 10
  • 47
  • 92
  • 2
    Perhaps it if related to this issue (the context is not the same): https://stackoverflow.com/a/49374128/793477?stw=2 – Ashton Thomas Mar 22 '18 at 12:06
  • 1
    Possible duplicate of [InheritedWidget with Scaffold as child doesn't seem to be working](https://stackoverflow.com/questions/49373774/inheritedwidget-with-scaffold-as-child-doesnt-seem-to-be-working) – Rémi Rousselet Mar 22 '18 at 12:35
  • See https://github.com/flutter/flutter/issues/15919, I think you're running into the same issue. – Kira May 29 '18 at 05:09

4 Answers4

15

The issue could be because of the MaterialApp context in the library. Creating a new Widget as home in MaterialApp may solve this

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new MaterialApp(
        home: new HomeScreen());
  }
}

class HomeScreen extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text("Title"),
      ),
      body: new Center(child: new Text("Click Me")),
      floatingActionButton: new FloatingActionButton(
        child: new Icon(Icons.add),
        backgroundColor: Colors.orange,
        onPressed: () {
          print("Clicked");
          Navigator.push(
            context,
            new MaterialPageRoute(builder: (context) => new AddTaskScreen()),
          );
        },
      ),
    );
  }
}
Adrian Mole
  • 49,934
  • 160
  • 51
  • 83
Abhishek Anand
  • 293
  • 5
  • 11
  • 1
    A link to a solution is welcome, but please ensure your answer is useful without it: [add context around the link](//meta.stackexchange.com/a/8259) so your fellow users will have some idea what it is and why it’s there, then quote the most relevant part of the page you're linking to in case the target page is unavailable. [Answers that are little more than a link may be deleted.](//stackoverflow.com/help/deleted-answers) – Blue Jul 25 '18 at 20:53
11

I struggled with this problem some days ago.

After a lot of exception trapping and printing, I solved a similar problem by wrapping all of the body property of the Scaffold in a Builder widget and using Builder's context as the context to use for Navigator operations.

More about this, with an explaination, can be found under the BuildContext documentation page:

https://docs.flutter.io/flutter/widgets/BuildContext-class.html

Following a similar pattern helped me solve my problem.

Edit: here's a code sample, where my build method returns a Builder that wraps actual wiget building.

  Widget build(BuildContext context) {
    // TODO: implement build
    return new Builder(
      builder: (ctx) {
        return new Scaffold(            
          key: scaffoldKey,
          appBar: new AppBar( 
            title: new Text ("${sector.name} Sector" ,
              style:  new TextStyle(
              fontFamily: "Exo",
              fontWeight: FontWeight.w900,
            )),
            actions: <Widget>[
              _menuSystemsList(ctx),
              _mainMenuActions(ctx) 
            ] ,
          ),
          body:  new Builder (
            builder: (ctx2) => _makeHexmapSector(ctx2)
          ),
        );
      }
    );
  }
Max Lambertini
  • 3,583
  • 1
  • 19
  • 24
5

I had same problem all i did was removed MaterialApp from class and moved it inside of runApp() in void main().

jagdish
  • 440
  • 6
  • 15
0

I had a similar kind of problem it exception was caught by the gesture detector BUT IT WASNT FOR NAVIGATOR PUSH

it was caused while dealing with JSON data inside of toJson, I had typed:

"price": this.img

instead of

"price": this.price
Tyler2P
  • 2,324
  • 26
  • 22
  • 31