skip to Main Content

I am trying to save a file to a path in windows using path_provider: 2.0.14. and everything works fine in debug, even when I use the app that is running by the command
"flutter run –release -d windows"

I mention this app is intended only for Windows.
Antivirus is disabled and the released windows app has all permissions.

What I already tried:
I already tried to see if there are any exceptions. There are none catched.
I already applied an implementation without path_provider. I hardocoded the path just so I see if it saves anything. It does in debug or release when started by the command above but not when started manually from build/windows/Runner/Release directory or from build/windows/Runner/debug

This is the actualy function:

void funcExample({required String choseName, required String chosenPath, required UInt8List fileAsBytes,}) {
     final docx = await DocxTemplate.fromBytes(fileAsBytes);

     final docxGeneratedBytes = await someMethodThatChangesDocx(docx);

     final path = "$chosenPath\$chosenName.docx";
  
     final XFile fileToBeSaved =
     XFile.fromData(Uint8List.fromList(docxGeneratedBytes),name: chosenName);
     await fileToBeSaved.saveTo(path);
}

I already tried to save by following snippet:

final XFile fileToBeSaved =
      XFile.fromData(Uint8List.fromList(docxGeneratedBytes),name: chosenName);
final newFileWithoutCross = File(path);
await newFileWithoutCross.writeAsBytes(await fileToBeSaved.readAsBytes());

When I try to open the release executable from build/windows/Runner/Release this saveTo method from the ‘cross_file’ library does not work. Also it does not throw any exception.

If we put a breakpoint at line 6: value path turns out to have a value for example: "C:UsersAcerDownloadsnewFile.docx"

Libraries that I use:
file_picker: 5.2.10
path_provider: 2.0.14
docx_template: 0.3.4
file_selector: 0.9.2+5

flutter doctor –verbose:
[√] Flutter (Channel stable, 3.7.12, on Microsoft Windows [Version 10.0.19045.3086], locale ro-RO)
• Flutter version 3.7.12 on channel stable at C:UsersAcerflutter
• Upstream repository https://github.com/flutter/flutter.git
• Framework revision 4d9e56e694 (3 months ago), 2023-04-17 21:47:46 -0400
• Engine revision 1a65d409c7
• Dart version 2.19.6
• DevTools version 2.20.1

[√] Windows Version (Installed version of Windows is version 10 or higher)

[!] Android toolchain – develop for Android devices (Android SDK version 33.0.2)
• Android SDK at C:UsersAcerAppDataLocalAndroidsdk
X cmdline-tools component is missing
Run path/to/sdkmanager --install "cmdline-tools;latest"
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/docs/get-started/install/windows#android-setup for more details.

[√] Chrome – develop for the web
• Chrome at C:Program Files (x86)GoogleChromeApplicationchrome.exe

[√] Visual Studio – develop for Windows (Visual Studio Community 2022 17.5.5)
• Visual Studio at C:Program FilesMicrosoft Visual Studio2022Community
• Visual Studio Community 2022 version 17.5.33627.172
• Windows 10 SDK version 10.0.22000.0

[√] Android Studio (version 2022.2)
• Android Studio at C:Program FilesAndroidAndroid Studio
• Flutter plugin can be installed from:
https://plugins.jetbrains.com/plugin/9212-flutter
• Dart plugin can be installed from:
https://plugins.jetbrains.com/plugin/6351-dart
• Java version OpenJDK Runtime Environment (build 17.0.6+0-b2043.56-9586694)

[√] Connected device (3 available)
• Windows (desktop) • windows • windows-x64 • Microsoft Windows [Version 10.0.19045.3086] • Chrome (web) • chrome • web-javascript • Google Chrome 114.0.5735.199
• Edge (web) • edge • web-javascript • Microsoft Edge 114.0.1823.67

[√] HTTP Host Availability
• All required HTTP hosts are available

! Doctor found issues in 1 category.

I expect that file is saved fine as it works in debug mode or when is ran by flutter command

2

Answers


  1. Chosen as BEST ANSWER

    The problem was when File object was created initially. The file objects created using paths from assets in release app should be done in a different way:

    if (kReleaseMode) {
        file = File('data\flutter_assets\lib\assets\$templateName.docx');
    } else {
        file = File('lib/assets/$templateName.docx');
    }
    

  2. Try using this new version path_provider, also I found this answer might help you answer,
    also try this package path_provider_windows work specific in windows might help you,
    otherwise, I recommend you start the issue in the path_provider Ropositry with all details to help fix the bug for other by contributors

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search