Search code examples
flutterdartfilepermissionsfilesystems

Flutter file word readable


I want to write a file with flutter, that has got the same function as WORLD_READABLE in android. So i want tha this file can be read by all the applications. More clear: i have an app A that write hello.txt i want that another app B can read this file. How i should set my file? There is a parameter of some function in the package path_provider?

I seen the package path_provider and File class, but i didn't find something. I have already tried by access direct to file from app B, but is not world readable.


Solution

  • Something like this should work and make sure you use the latest path_provider dependency. getExternalStorageDirectory() does the trick!

    //Write file from App 1
    
     import 'dart:io';
     import 'package:path_provider/path_provider.dart';
        
     Future<void> writeToFile(String data) async {
       final directory = await getExternalStorageDirectory();
       final file = File('${directory.path}/hello.txt');
       await file.writeAsString(data);
        }
    
    //Read the written file from App 2
    
    import 'dart:io';
    import 'package:path_provider/path_provider.dart';
    
    Future<String> readFromFile() async {
      final directory = await getExternalStorageDirectory();
      final file = File('${directory.path}/hello.txt');
      return file.readAsString();
    }