DEV Community

Kaede Games ๐ŸŽฎ
Kaede Games ๐ŸŽฎ

Posted on

Flutter ๐Ÿงข Best code to share by Twitter and Line

There are multiple REST APIs for sharing Twitter and LINE.

But some are not fully supporting native mobiles, Android, and iOS. For example, some are not working as deep-link, or force hashtags to raw string.

Donโ€™t mind, I found the best APIs and write the codes to share by Flutter below.

Check it๐Ÿ‘‡


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

Enter fullscreen mode Exit fullscreen mode

THX๐Ÿ’œ

Top comments (0)