10

I am developing an app using Flutter. Now I need to render the HTML content in my application that includes anchor links. But there is a problem with the links in the HTML content, it is not opening the links in the browser when it is clicked.

This is my code.

Container(
                              color: Colors.white,
                              padding: EdgeInsets.only(top: 10, right: 10, bottom: 10, left: 10),
                              child: Html(
                                  data: htmlContent,
                                  onLinkTap: (link) {
                                    launch(link);
                                  },
                              ))

When I click on it, it is not working.

I imported this library at the top.

import 'package:url_launcher/url_launcher.dart';

What is wrong with my code and how can I fix it?

1
  • 1
    It is easier if you provide a working example or more information (What is the content of htmContent? What is the thrown exception? I assume you also use the flutter_html package.). Assuming you have a MissingPluginException: Closing the app and executing flutter run (in the cli) should work. Commented Oct 15, 2020 at 5:15

4 Answers 4

11
 Html(
  data: dataHTML,
  onLinkTap: (url, _, __, ___) async {
    if (await canLaunch(url!)) {
      await launch(
        url,
      );
    } 
  },
);

In recent versions, flutter_html, when using this event, requests more parameters that fulfill a specific task which is detailed in the documentation, but if the intention is only to launch the Urls, they can do it this way, do not forget to add the <queries> in the AndroidManifest if your app uses API 30 or higher that you specify in using url_launcher

Sign up to request clarification or add additional context in comments.

Comments

6

Opening links in HTML works for me like this:

Html(
  data: 'This is some <a href="https://pub.dev/packages/url_launcher">link</a>',
  onLinkTap: (url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
      );
    } else {
      throw 'Could not launch $url';
    }
  },
)

Note that I first await the canLaunch method before using launch to open my link.

What was your link? Would you provide us an example? Thanks!

Comments

2
onLinkTap: (url) async {
    if (await canLaunch(url)) {
      await launch(
        url,
      );
    } else {
      throw 'Could not launch $url';
    }
  },

Also don't forgot to add this in your Manifest file

       <queries>
        <intent>
            <action android:name="android.intent.action.VIEW" />
            <data android:scheme="https" />
        </intent>
        <intent>
            <action android:name="android.intent.action.DIAL" />
            <data android:scheme="tel" />
        </intent>
        <intent>
            <action android:name="android.intent.action.SEND" />
            <data android:mimeType="*/*" />
        </intent>
    </queries>

Comments

2

If you are using flutter_html Prerelease: 3.0.0-beta.2 use the following code.

  Html(
     data: loadHtml(currentEvent.description),
     onLinkTap: (url, _, __) async {
         final uri = Uri.parse(url!);
         if (await canLaunchUrl(uri)) {
             await launchUrl(uri);
         }
      },
  ),

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.