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)