简体   繁体   English

Flutter dart: Url launcher Not throw error Page

[英]Flutter dart : Url launcher Not throw error Page

I purposely put the wrong url so I can see my error page, but it doesn't show anything我故意输入错误的 url 这样我就可以看到我的错误页面,但它没有显示任何内容

this my code这是我的代码




class DetailPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    // final Uri _url = Uri.parse('https://goo.gl/maps/HiKcRmtjE2if58aw7');
    final Uri _url = Uri.parse('adjja');

    final Uri _wa = Uri.parse('https://wa.me/62895378147657');

    
    
    Future<void> _launchUrl() async {
      if (!await launchUrl(_url)) {
        throw Navigator.push(
            context, MaterialPageRoute(builder: (context) => ErrorPage()));
      }
    }

   

    

Use try and catch block to handle the error because due to some unresolved intent problem in the package, it is not able to throw an error.使用 try 和 catch 块来处理错误,因为由于 package 中的一些未解决的意图问题,它无法抛出错误。 Also while navigating you should only use the push function and not the throw prefix like the following code: -此外,在导航时,您应该只使用 push function 而不是像以下代码那样的 throw 前缀:-

Future<void> _launchUrl() async {
try {
  await launchUrl(_url);
} catch (e) {
  Navigator.push(
      context, MaterialPageRoute(builder: (context) => ErrorPage()));
  }
}

remove throw and You can check if url is valid or not by using canLaunchUrl method删除抛出,您可以使用 canLaunchUrl 方法检查 url 是否有效

 if (await canLaunchUrl(url)) {
  await launchUrl(
    url,
    mode: _useWebView
        ? LaunchMode.inAppWebView
        : LaunchMode.externalApplication,
  );
} else {
  Navigator.push(
        context, MaterialPageRoute(builder: (context) => ErrorPage()));
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM