简体   繁体   中英

How to solve the hyperlink error in flutter? (it shows me just a white screen)

I used url_launcher package to make hyperlink in flutter app.

Widget build(BuildContext context) {
    final Size size = MediaQuery.of(context).size;
    List<String> dataList = data.split('@@');

    return Scaffold(
      backgroundColor: Color(0xffd6d6d6),
      appBar: AppBar(
        backgroundColor: Color(0xff3D1472),
        elevation: 0,
      ),
      drawer: AppDrawer(),
      body: SingleChildScrollView(
          child: Column(
        children: <Widget>[
          Stack(
            children: <Widget>[
              Head(size: size),
              Column(
                children: <Widget>[
                  Padding(
                    padding: EdgeInsets.only(
                        top: 5,
                        left: size.width * 0.2,
                        right: size.width * 0.2),
                    child: NewsHead(
                      size: size,
                      dataList: dataList,
                      imgNum: 0,
                      titleNum: 10,
                      urlNum: 20,
                    ),
                  ),

above is the part of main code.

and this is the code of NewsHead.

class NewsHead extends StatefulWidget {
  final Size size;
  final List<String> dataList;
  final int imgNum;
  final int titleNum;
  final int urlNum;

  const NewsHead({this.size, this.dataList, this.imgNum, this.titleNum, this.urlNum});

  @override
  _NewsHeadState createState() => _NewsHeadState();
}

class _NewsHeadState extends State<NewsHead> {
  Future<void> _launched;

  @override
  Widget build(BuildContext context) {
    Size size = widget.size;
    List<String> dataList = widget.dataList;
    int imgNum = widget.imgNum;
    int titleNum = widget.titleNum;
    int urlNum = widget.urlNum;

   
    return Column(
      children: [
        Image.network(
          dataList[imgNum],
          width: size.width * 0.6,
          height: size.height * 0.25,
          fit: BoxFit.fill,
        ),
        SizedBox(
          height: size.height*0.05+100,
          child: RaisedButton(
              onPressed: () => setState(() {
                _launched = _launchURL(dataList[urlNum]);
              }),
              color: Colors.transparent,
              elevation: 0,
              child: Text(
                dataList[titleNum],
                style: TextStyle(fontSize: 20, color: Colors.white70),
              )),
        ),
      ],
    );

below is the code of _launchURL.

Future<void> _launchURL(String url) async {
  if (await canLaunch(url)) {
    await launch(
      url,
      forceSafariVC: true,
      forceWebView: true,
      headers: <String, String>{'my_header_key': 'my_header_value'},
    );
  } else {
    throw 'Could not launch $url';
  }
}

I followed the example _launchInWebViewOrVCcode in here( https://pub.dev/packages/url_launcher/example )

When I clicked the text which linked to the site, it just shows me a white screen with this.

Performing hot reload...
Syncing files to device Android SDK built for x86...
Reloaded 0 of 539 libraries in 140ms.
D/EGL_emulation( 6934): eglMakeCurrent: 0xa9485240: ver 2 0 (tinfo 0xa9483a00)
E/eglCodecCommon( 6934): glUtilsParamSize: unknow param 0x000085b5
E/eglCodecCommon( 6934): glUtilsParamSize: unknow param 0x000085b5
D/EGL_emulation( 6934): eglMakeCurrent: 0x863f6140: ver 2 0 (tinfo 0x8187ef10)
D/EGL_emulation( 6934): eglMakeCurrent: 0x863f6140: ver 2 0 (tinfo 0x8187ef10)
E/eglCodecCommon( 6934): glUtilsParamSize: unknow param 0x000085b5
I/chatty  ( 6934): uid=10083(u0_a83) RenderThread identical 18 lines
E/eglCodecCommon( 6934): glUtilsParamSize: unknow param 0x000085b5
E/eglCodecCommon( 6934): glUtilsParamSize: unknow param 0x000085b5
E/eglCodecCommon( 6934): glUtilsParamSize: unknow param 0x000085b5

also it told me that "the value of the field '_launched' isn't used". How can I solve this problem?

It's possible that the URL is loading correctly, but depending on the site you're trying to display, you might want to enable Javascript, which is disabled by default.

Try adding enableJavaScript: true, to the launch function.

Future<void> _launchURL(String url) async {
  if (await canLaunch(url)) {
    await launch(
      url,
      forceSafariVC: true,
      forceWebView: true,
      enableJavaScript: true,
      headers: <String, String>{'my_header_key': 'my_header_value'},
    );
  } else {
    throw 'Could not launch $url';
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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