flutter - 在颤振中使用 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(),
解决方案
在返回 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,
),
推荐阅读
- javascript - 离子 SQLite ; 无法打开数据库
- logging - 如何使用 Terraform 11.14 启用 Azure Key Vault 日志记录?
- ios - 如何从 Firestore 返回数组中的数据?
- java - 如何启用单击按钮格式,并使用窗口增加文本大小
- python - 如何检查我的停用词删除算法是否有效?
- python - 遍历目录中的所有文件夹和文件
- angular - 如何通过 SPA 中的组件重定向到 payulatam
- discord.js - 如何修复“无法读取未定义的属性‘toLowerCase’”
- html - div元素不能并排放置的问题
- mysql - 在 SQL 嵌套集层次结构中查找链中所有父项的集合以获取多个提示