fluttersaf

How to use flutter Saf package?


I have read flutter saf package documentation from here SAF, but my problem now is that every time app is asking to provide permission even if I have already provided it. I think I'm missing something here about how to handle permission using saf package can anyone help please?

Here is what I did up to now.

  Future<int> path() async {
    DeviceInfoPlugin deviceInfo = DeviceInfoPlugin();
    AndroidDeviceInfo androidInfo = await deviceInfo.androidInfo;
    if (androidInfo.version.sdkInt >= 29) {
      Permission.storage.request();
      await saf.getDirectoryPermission(isDynamic: true);
      var isSync = await saf.sync();
      if (isSync as bool) {
        var _paths = await saf.getCachedFilesPath();
        loadImage(_paths);
      }
      return 1;
     
    } else {
      return 0;
     
    }
  }

And here is how i use it

  @override
  Widget build(BuildContext context) {
    return FutureBuilder(
        future: path(),
        builder: (BuildContext context, AsyncSnapshot<int> text) {
          if (text.connectionState == ConnectionState.waiting) {
            return const Scaffold(
              body: Center(
                child: Text("Loading....",style: TextStyle(color: Color(0xff090849),fontSize: 20),),
              ),
            );
          } else {
              return  Scaffold(
                  body:  GridView.builder(
                  padding: const EdgeInsets.all(3.0),
                  itemCount: _paths.length,
                  gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                    crossAxisCount: 3,
                    childAspectRatio: 3/4,
                    ), 
                  itemBuilder: 
                  (BuildContext context, int index) {
                    return Padding(
                      padding: const EdgeInsets.all(5.0), // Adjust the value as needed
                      child: Material(
                        borderRadius: BorderRadius.circular(10),
                        elevation: 5,
                        clipBehavior: Clip.antiAliasWithSaveLayer,
                        color: Colors.teal.withOpacity(0.9),
                        child: InkWell(
                        splashColor: Colors.teal,
                        onTap: () {
                         Navigator.push(
                          context,
                          MaterialPageRoute(
                            builder: (_) => ImageView(imagePath: _paths[index], key: UniqueKey()),
                          ),
                        );
                        },
                       child: Column(
                        mainAxisSize: MainAxisSize.min,
                        children: [
                          SizedBox(
                            height: 110, 
                            child: Ink.image(
                              image: FileImage(File(_paths[index])),
                              fit: BoxFit.cover,
                            ),
                          ),
                          MaterialButton(
                            height: 10,
                            onPressed: () async {
                              saveFile(_paths[index]);
                          },child: const Text("Download",style: TextStyle(color: Colors.white),),)
                        ],
                       ),
                      ),
                     )
                    );
                   },
                  ),
                );
            }
         });
     }


Solution

  • For anyone facing a similar issue like mine, the solution is using the directory like this:

    "Android/yourpath"

    Not like this:

    "/storage/emulated/0/Android/yourpath"