Skip to content

[Bug]: Teams app file attachment issue using share_plus #3934

Description

@ketan-cg

Platform

Web (Chrome) Version 150.0.7871.129 (Official Build) (64-bit)

Plugin

share_plus

Version

12.0.2

Flutter SDK

3.44.4

Steps to reproduce

The issue appears to be related to the Teams cache. When I attach the same files using Outlook, they are attached successfully every time without any issues. There are no cases where two files are attached or where no file is attached. The attachment always matches the selected files. I have tried multiple solutions but it didn't work.

Solution 1: Use Unique File Paths & Force Cache Deletion (If Sharing Files)

If you are sharing files (like a PDF or CSV), Teams fails on the second attempt because the file name or path is identical, causing a cache collision in Teams. You must randomize the file name every single time using a timestamp or UUID.

import 'dart:io';import 'package:path_provider/path_provider.dart';
import 'package:share_plus/share_plus.dart';

Future<void> shareToTeamsSafely(List<int> fileBytes) async {
  final tempDir = await getTemporaryDirectory();
  
  // 1. Force a completely unique file name every single timefinal timestamp = DateTime.now().millisecondsSinceEpoch;
  final file = File('${tempDir.path}/report_$timestamp.csv'); 
  
  await file.writeAsBytes(fileBytes);

  // 2. Share the unique fileawait Share.shareXFiles([XFile(file.path)]);
  
  // 3. Clear your local reference after a slight delay so Teams can read it  Future.delayed(Duration(seconds: 10), () async {
    if (await file.exists()) {
      await file.delete();
    }
  });
}

Solution 2: Deep Link directly via URL Launcher (If Sharing Text/Links)

If you are just sharing text or a URL, completely skip the system share sheet (and share_plus). Launching a direct Microsoft Teams deep link URL resets the Teams application state on every click, forcing it to open a fresh thread window.

Install url_launcher and route the message directly:

import 'package:url_launcher/url_launcher.dart';

Future<void> shareToTeamsDirectly(String message) async {
  // Encode your text message safely for URLsfinal encodedMessage = Uri.encodeComponent(message);
  
  // Deep link format for launching MS Teams chat/share channelfinal teamsUri = Uri.parse("https://microsoft.com");

  if (await canLaunchUrl(teamsUri)) {
    await launchUrl(teamsUri, mode: LaunchMode.externalApplication);
  } else {
    // Fallback if Teams app isn't installed on the deviceawait launchUrl(Uri.parse("https://live.com")); 
  }
}

Solution 3: The Native Reset Workaround (For Android Only)

If you are on Android and must use the native system sheet, the issue happens because the Intent.ACTION_SEND chooser flags aren't completely cleared by the OS when Teams remains running in the background. You can fix this by writing a custom Android Intent handler that flags the system to clear the task history on the second trigger.

Replace your Flutter share invocation with a method channel calling this native Android Kotlin code:

val sendIntent = Intent().apply {
    action = Intent.ACTION_SEND
    putExtra(Intent.EXTRA_TEXT, "Your share text")
    type = "text/plain"// Crucial flags to tell Android and Teams to open a brand new instance    addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
    addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET) 
}val chooser = Intent.createChooser(sendIntent, "Share via")
context.startActivity(chooser)

Code Sample

Logs

Error: Failed to execute 'share' on 'Navigator'

Flutter Doctor

[√] Flutter (Channel stable, 3.44.4, on Microsoft Windows [Version 10.0.26200.8655], locale en-IN) [2.3s]
    • Flutter version 3.44.4 on channel stable at C:\flutter_sdk\flutter_windows_3.44.4-stable\flutter
    • Upstream repository https://github.com/flutter/flutter.git
    • Framework revision ad70ec4617 (4 weeks ago), 2026-06-24 11:07:06 -0700
    • Engine revision a10d8ac38d
    • Dart version 3.12.2
    • DevTools version 2.57.0
    • Feature flags: enable-web, enable-linux-desktop, enable-macos-desktop, enable-windows-desktop, enable-android, enable-ios, cli-animations,
      enable-native-assets, enable-swift-package-manager, omit-legacy-version-file, enable-lldb-debugging, enable-uiscene-migration

[√] Windows Version (Windows 11 or higher, 25H2, 2009) [3.9s]

[!] Android toolchain - develop for Android devices (Android SDK version 36.1.0) [2.1s]
    • Android SDK at C:\Users\kketanbh\AppData\Local\Android\sdk
    • Emulator version 36.5.10.0 (build_id 15081367) (CL:N/A)
    X cmdline-tools component is missing.
      Try installing or updating Android Studio.
      Alternatively, download the tools from https://developer.android.com/studio#command-line-tools-only and make sure to set the ANDROID_HOME
      environment variable.
      See https://developer.android.com/studio/command-line for more details.
    X Android license status unknown.
      Run `flutter doctor --android-licenses` to accept the SDK licenses.
      See https://flutter.dev/to/windows-android-setup for more details.

[√] Chrome - develop for the web [717ms]
    • Chrome at C:\Program Files\Google\Chrome\Application\chrome.exe

[√] Visual Studio - develop Windows apps (Visual Studio Professional 2022 17.14.31 (April 2026)) [715ms]
    • Visual Studio at C:\Program Files\Microsoft Visual Studio\2022\Professional
    • Visual Studio Professional 2022 version 17.14.37216.2
    • Windows 10 SDK version 10.0.26100.0

[√] Connected device (3 available) [1,702ms]
    • Windows (desktop) • windows • windows-x64    • Microsoft Windows [Version 10.0.26200.8655]
    • Chrome (web)      • chrome  • web-javascript • Google Chrome 150.0.7871.129
    • Edge (web)        • edge    • web-javascript • Microsoft Edge 150.0.4078.83

[√] Network resources [1,715ms]
    • All expected network resources are available.

! Doctor found issues in 1 category.

Checklist before submitting a bug

  • I searched issues in this repository and couldn't find such bug/problem
  • I Google'd a solution and I couldn't find it
  • I searched on StackOverflow for a solution and I couldn't find it
  • I read the README.md file of the plugin
  • I'm using the latest version of the plugin
  • All dependencies are up to date with flutter pub upgrade
  • I did a flutter clean
  • I tried running the example project

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions