gpt4 book ai didi

Flutter:向用户显示错误

转载 作者:IT王子 更新时间:2023-10-29 07:08:43 26 4
gpt4 key购买 nike

我有一个 url_launcher,它打开一个电子邮件应用程序和一个 onTap 监听器,如果设备上没有安装电子邮件应用程序,我想显示一个错误?如何使用 Flutter 框架实现这一点?

当电子邮件应用程序不存在时,_launchcaller() 会抛出异常,但我希望向用户显示错误消息(自定义消息),以便他们知道发生了什么?

@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("Thank you"),
),
body: new ListView(children: <Widget>[
new Column(
children: <Widget>[
new Padding(
padding: new EdgeInsets.all(20.0),
child: new AnimatedBuilder(
animation: animationController,
child: new Container(
height: 150.0,
width: 150.0,
child: new Image.asset('assets/angry_face.png'),
),
builder: (BuildContext context, Widget _widget) {
return new Transform.rotate(
angle: animationController.value * 6.3,
child: _widget,
);
},
),
),
new Column(
children: <Widget>[
new Text(
'We are sorry for the inconvenience...',
style: new TextStyle(
fontSize: 16.0,
fontWeight: FontWeight.w700,
color: Colors.black87),
),
new Padding(
padding: new EdgeInsets.symmetric(
vertical: 5.0, horizontal: 0.0)),
new Text(
'Please contact us to resolve this issue!',
style: new TextStyle(
fontSize: 13.0,
fontWeight: FontWeight.w200,
color: Colors.black87),
),
],
),
new Padding(
padding:
new EdgeInsets.symmetric(vertical: 25.0, horizontal: 0.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
crossAxisAlignment: CrossAxisAlignment.center,
children: <Widget>[
new GestureDetector(
onTap: _launchcaller,
child: new Icon(
Icons.contact_phone,
color: Colors.green,
size: 45.0,
),
),
new GestureDetector(
onTap: _launchcaller,
child: new Icon(
Icons.email,
color: Colors.blue,
size: 50.0,
),
),
],
),
),
],
),
]));
}

_launchcaller() async {
const url = 'mailto:enissl21@gmail.com?subject=subject&body=body';

if (await canLaunch(url)) {
await launch(url);
} else {
throw 'Could not launch $url';
}
}
}

最佳答案

除非那不是一个选择,否则您可以像这样显示一个 snackbar 。

你有一个脚手架,所以给它添加一个键

final GlobalKey<ScaffoldState> _scaffoldKey = new GlobalKey<ScaffoldState>();


Scaffold(
key: _scaffoldkey,
...

然后像这样抛出错误:

_launchcaller() async {
const url = 'mailto:enissl21@gmail.com?subject=subject&body=body';

if (await canLaunch(url)) {
await launch(url);
} else {
scaffoldkey.currentState.showSnackBar(
SnackBar(
content: new Text('Error: ${url}'),
duration: new Duration(seconds: 10),
);
);
throw 'Could not launch $url'; // <-- you might wanna ignore/comment this part out
// Or use a `print('Could not launch $url')` instead.
print('Could not launch $url')
}
}

关于Flutter:向用户显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51565959/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com