首页 > 解决方案 > 在颤振中使用 showDialog 会引发错误 - “'!_debugLocked': is not true.”

问题描述

我有一个计数器变量,每次用户打开页面时都会递增。当页面第 5 次打开时,我想显示一个对话框。所以我在这种情况下使用了 showDialog 。我在函数中使用了 showDialog。并在 if else 简写中使用它。该应用程序第 5 次显示以下错误 -

'package:flutter/src/widgets/navigator.dart':断言失败:第 5253 行 pos 12:'!_debugLocked':不正确。

这是代码-

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(),

标签: fluttershowdialog

解决方案


在返回 await showDialog(....); 之前,我必须添加以下代码行

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

并使用 Futurebuilder 而不是直接调用该函数。

(_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,
            ),

推荐阅读