Qiita Teams that are logged in
You are not logged in to any team

Log in to Qiita Team
Community
OrganizationEventAdvent CalendarQiitadon (β)
Service
Qiita JobsQiita ZineQiita Blog
1
Help us understand the problem. What are the problem?

More than 1 year has passed since last update.

posted at

updated at

Flutterでテキスト内のURLをチェックする方法

ListViewでデータを表示する前にテキストの中にURLが含まれているかどうかのチェックをしてから、その結果に対しての表示を変更させるのに使いました。
調べた時に日本語の説明がなかったので短いですが備忘録として!

final urlRegExp = RegExp(
          r"((https?:www\.)|(https?:\/\/)|(www\.))[-a-zA-Z0-9@:%._\+~#=]{1,256}\.[a-zA-Z0-9]{1,6}(\/[-a-zA-Z0-9()@:%_\+.~#?&\/=]*)?");
      final urlMatches = urlRegExp.allMatches(message);
      List<String> urls = urlMatches
          .map((urlMatch) =>
              message.substring(urlMatch.start, urlMatch.end))
          .toList();
      urls.forEach((value) {
        if (value != null) {
          // 処理
        } else {
          // 処理
        }
      });

参考文献

RegExp class
Flutter - Validating a string for URL

Why not register and get more from Qiita?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
1
Help us understand the problem. What are the problem?