Firus
Firus

Reputation: 559

Flutter - Save a local text file visible for user on Android

I'm trying to create a text file in a folder where the user can see for the file explorer apps of the mobile device, preferably in Documents folder.

Here is what I've till now (consider that we have all required permissions)

  final directory = Platform.isAndroid
      ? await getExternalStorageDirectory()
      : await getApplicationDocumentsDirectory();

  final filePath = '${directory!.path}/example.txt';

  File file = File(filePath);
  await file.create();
  await file.writeAsString('Some random text content');

After that I try to find this new file on my file explorer apps, but it seems to not exists and no error logs can been seen.

If you want see an example code I wrote this PoC -> https://github.com/felipeemidio/save-local-file-poc

What I've tried so far?

Upvotes: 0

Views: 1555

Answers (1)

Kaushik Chandru
Kaushik Chandru

Reputation: 17732

Try file path as

String filePath = '/storage/emulated/0/Downloads/example.text';

and don't forget to add permissions in manifest

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 2

Related Questions