5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Flutter 🧢 LINE とTwitterでシェアするコードのベストプラクティス

Posted at

本編

ライン、Twitter には、複数のシェア用 REST API が用意されているが、Android, iOSにネイティブ対応されたものを以下に示した。(その他のAPI は、ディープリンク対応していなかったり、ハッシュタグが機能しなかったりする。)


Future<void> shareByLine(String text) async {
  final lineUrl = Uri(
    scheme: 'https',
    host: 'line.me',
    path: 'R/msg/text/' + text,
  );
  await launchUrl(lineUrl, mode: LaunchMode.externalApplication);
}

Future<void> shareByTwitter(String text) async {
  final tweetQuery = <String, dynamic>{
    'text': text,
    // 'url': 'https://...',
    // 'hashtags': ['tag1', 'tag2'],
  };

  final twitterUrl = Uri(
    scheme: 'https',
    host: 'twitter.com',
    path: 'intent/tweet',
    queryParameters: tweetQuery,
  );

  await launchUrl(twitterUrl, mode: LaunchMode.externalApplication);
}

String? encodeQueryParameters(Map<String, String> params) {
  return params.entries
      .map((MapEntry<String, String> e) =>
          '${Uri.encodeComponent(e.key)}=${Uri.encodeComponent(e.value)}')
      .join('&');
}

カンペキ!💜

5
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?