LoginSignup
1
1

More than 1 year has passed since last update.

Flutterからtwitterにシェアする

Posted at

表題の通り、めっちゃハマったので、記録しておく。

要件

flutterからTwitterにシェアするとき、アプリをインストールしていればアプリへ、そうでなければwebへ。
シェアする内容はtext, url, hashtags

事前準備

  1. iOSの設定: info.plistにschemeを登録
<key>LSApplicationQueriesSchemes</key>
	<array>
     ...
     <string>twitter</string>
    </array>
  1. Androidの設定:sdk30以降必要。AndroidManifest.xmlに以下を追加(他のschemeへ飛べるための設定)
    <intent>
        <action android:name="android.intent.action.SEND" />
        <data android:mimeType="*/*" />
    </intent>

シェアする時

twitter://postする時、urlhashtagsは認識されないので、textにそのまま追加

    final tweetQuery = <String, dynamic>{
      'text': 'myText',
      'url': 'https://...',
      'hashtags': ['tag1', 'tag2'],
    };

    final text = 'myText'
        ' #tag1 #tag2'
        ' ${https://...}';
    final tweetScheme = Uri(
      scheme: 'twitter',
      host: 'post',
      queryParameters: <String, String>{'text': text},
    );
    final tweetIntentUrl =
        Uri.https('twitter.com', '/intent/tweet', tweetQuery);
    await canLaunchUrl(tweetScheme)
        ? await launchUrl(tweetScheme)
        : await launchUrl(tweetIntentUrl);
1
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
1
1