Flutter:向用户显示错误

时间:2018-07-27 21:49:26

标签: flutter

我有一个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';
    }
  }
}

1 个答案:

答案 0 :(得分:2)

除非没有选择,否则您可以显示一个小吃店。

您有一个Scaffold,因此在其中添加密钥

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')
    }
  }