LoginSignup
3
1

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('&');
}

カンペキ!💜

3
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
3
1