Flutter: Using the url_launcher’s onLinkTap method (example, syntax)

As a brief note, I ended up not using the following code, but if you ever need to use the Flutter url_launcher library with the Flutter Html widget’s onLinkTap method, this code should work for you:

child: Html(
  data: _aboutText,
  onLinkTap: (url, _, __, ___) {
    print("Opening $url...");
    launchURL(url!);
  },
),


// elsewhere in your code, such as a net_utils.dart file
import 'package:url_launcher/url_launcher.dart';

launchURL(String url) async {
    if (await canLaunch(url)) {
        await launch(url);
    } else {
        //TODO handle this
        throw 'Could not launch $url';
    }
}

If I had spent more time with this I would clean up that launchURL function, but hopefully it can be helpful as-is. The important part of it is shown, that you’ll probably want to use (or need to use) the async/await approach.

Note that you can also use variable names in the onLinkTap method handler — i.e., where I show the url variable — but because I don’t need those other variables for what I’m doing, I skip those fields.

Again, if you ever need to use the url_launcher library with the Flutter Html widget’s onLinkTap method, I hope this example is helpful.