Flutter:弹出 2 个警报对话框时应用程序崩溃
Posted
技术标签:
【中文标题】Flutter:弹出 2 个警报对话框时应用程序崩溃【英文标题】:Flutter: app crashes when 2 alert dialogs are pops 【发布时间】:2021-07-24 12:14:00 【问题描述】:如果快速点击两张卡,会出现黑屏,需要重启应用
Flutter 2.0.5 • 通道稳定 • https://github.com/flutter/flutter.git 框架 • 修订版 adc687823a(2 周前) • 2021-04-16 09:40:20 -0700 引擎 • 修订版 b09f014e96 工具• Dart 2.12.3
脚手架
class HomePage extends StatelessWidget
@override
Widget build(BuildContext context)
return Scaffold(
body: Container(
child: Column(
children: [
GestureDetector(
child: Container(
child: GFCard('The quick brown fox jumps over the lazy dog')),
onTap: Alerts.correct(context),
),
GestureDetector(
child: GFCard('The quick brown fox jumps over the lazy dog'),
onTap: Alerts.wrong(context),
),
],
),
));
提醒
import 'package:flutter/material.dart';
import 'dart:async';
class Alerts
static correct(BuildContext context)
return () => _alert(context,
backgroundColor: Color(0xFF25C047), message: 'Correct ????');
static wrong(BuildContext context)
return () => _alert(context,
backgroundColor: Color(0xFFC22121), message: 'Wrong :(');
static _alert(BuildContext context, Color backgroundColor, String message)
return showDialog(
barrierColor: Colors.transparent,
context: context,
builder: (BuildContext context)
Timer(Duration(milliseconds: 500), () => Navigator.of(context).pop());
return AlertDialog(
insetPadding: EdgeInsets.all(35),
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.all(Radius.circular(16))),
content: Text(
message,
style: TextStyle(
color: Colors.white,
fontSize: 24,
),
textAlign: TextAlign.center,
),
elevation: 24,
backgroundColor: backgroundColor,
// shape: RoundedRectangleBorder(),
);
,
);
错误
E/flutter ( 5535): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: Looking up a deactivated widget's ancestor is unsafe.
E/flutter ( 5535): At this point the state of the widget's element tree is no longer stable.
E/flutter ( 5535): To safely refer to a widget's ancestor in its dispose() method, save a reference to the ancestor by calling dependOnInheritedWidgetOfExactType() in the widget's didChangeDependencies() method.```
【问题讨论】:
【参考方案1】:要解决这个问题,您应该在 showDialog() 中设置 barrierDismissible: false
【讨论】:
以上是关于Flutter:弹出 2 个警报对话框时应用程序崩溃的主要内容,如果未能解决你的问题,请参考以下文章