Search code examples
fluttershowdialog

Using showDialog in flutter is throwing error - "' !_debugLocked': is not true."


I have a counter variable that is incremented every time a user opens a page. When the page is opened for 5th time I want to show a dialog. So I have used showDialog in this case. I have used showDialog inside a function. And used it in a if else short hand. In the 5th time the app is showing the following error -

'package:flutter/src/widgets/navigator.dart': Failed assertion: line 5253 pos 12: '!_debugLocked': is not true.

Here is the code-

Future<Widget> _showDialog(BuildContext c) async {
return await showDialog(
    context: c,
    barrierDismissible: false,
    builder: (c) {
      return AlertDialog(
        elevation: 24.0,
        backgroundColor: Colors.blue,
        title: Text(
          'Liking our app?',
          textAlign: TextAlign.center,
          style: TextStyle(
            fontWeight: FontWeight.bold,
            color: Colors.white,
          ),
        ),
        actions: <Widget>[
          TextButton(
            child: Text(
              "Continue as Guest",
              style: TextStyle(
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            onPressed: () {},
          ),
          TextButton(
            child: Text(
              "Sign Up",
              style: TextStyle(
                fontWeight: FontWeight.bold,
                color: Colors.white,
              ),
            ),
            onPressed: () {},
          ),
        ],
      );
    });

}

Text("$_counter", textAlign: TextAlign.center),
      (_counter == 5) ? _showDialog(context) : SizedBox(),

Solution

  • I had to add the following line of code before returning await showDialog(....);

     await Future.delayed(Duration(seconds: 1));
    

    And used Futurebuilder instead of directly calling the function.

    (_counter == 8)
              ? FutureBuilder<Widget>(
                  future: _showDialog(context),
                  builder: (context, AsyncSnapshot<Widget> snap) {
                    if (snap.hasData) {
                      return snap.data;
                    } else {
                      //return CircularProgressIndicator();
                      return Container(height: 0.0, width: 0.0);
                    }
                  })
              : Container(
                  height: 0.0,
                  width: 0.0,
                ),