native_social_share 0.0.2
native_social_share: ^0.0.2 copied to clipboard
Native Social Share is a Flutter plugin that enables easy file sharing to popular social media platforms directly from your app. Whether you're looking to share images, text, or other file types, this [...]
Native Social Share Plugin #
Overview #
The Native Social Share plugin allows Flutter developers to easily share files (images, text, etc.) to popular social media platforms such as Facebook, Twitter, WhatsApp, and more. This plugin provides native functionality to share content directly from your app.
Setup Instructions #
Step 1: Add file_paths.xml #
For the plugin to properly share files, you need to configure file access via a FileProvider. This requires adding a file_paths.xml file to your Android project.
- Create a new XML file in your project under the path
android/app/src/main/res/xml/and name itfile_paths.xml. - Copy the following content into the
file_paths.xmlfile:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<paths>
<root-path
name="root"
path="." />
<external-path
name="external"
path="." />
<external-files-path
name="external_files"
path="." />
<cache-path
name="cache"
path="." />
<external-cache-path
name="external_cache"
path="." />
<files-path
name="files"
path="." />
</paths>
</resources>
This configuration allows the FileProvider to grant URI access to various paths, such as external storage, cache, and app files.
Step 2: Update AndroidManifest.xml #
Next, you need to configure the FileProvider in your AndroidManifest.xml file to work with the paths you've just defined.
- Open the
AndroidManifest.xmlfile located atandroid/app/src/main/AndroidManifest.xml. - Add the following provider entry inside the
<application>tag:
<provider
android:name="androidx.core.content.FileProvider"
android:authorities="${applicationId}.provider"
android:exported="false"
android:grantUriPermissions="true">
<meta-data
android:name="android.support.FILE_PROVIDER_PATHS"
android:resource="@xml/file_paths" />
</provider>
This tells Android that the FileProvider should use the paths defined in file_paths.xml to share files. The android:authorities attribute must match the application ID defined in your build.gradle file, which is usually ${applicationId}.
Step 3: Flutter Plugin Installation #
- Add the plugin to your
pubspec.yamlfile:
dependencies:
native_social_share: ^1.0.0
- Run the following command to install the plugin:
flutter pub get
Step 4: Usage Example #
After setting up the plugin, you can start using it to share content. Here is an example of how to use the plugin:
import 'package:native_social_share/native_social_share.dart';
// Share content
NativeSocialShare.shareContent("Share this text", "image.jpg");
Step 5: Additional Configuration #
Ensure that the necessary permissions are added to your AndroidManifest.xml for accessing storage and sharing files. This is particularly important for Android versions before Android 10.
Example permissions:
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_MEDIA_IMAGES" />
Troubleshooting #
-
Error:
FileProvidersetup issues Ensure that yourfile_paths.xmlis correctly placed inandroid/app/src/main/res/xml/and that the authority in the manifest matches the application ID. -
Permissions not granted If you're testing on Android 10 or above, ensure you have requested the appropriate runtime permissions using the
permission_handlerpackage or similar methods.
License #
This plugin is released under the MIT License. See the LICENSE file for more details.