How to Get Path of App File System in Flutter

In this post, we are going to show you how to get an App file system like a temporary directory and app document directory. Files system directories are important to read and write files from Flutter. See the examples below:

First, add path_provider package in your project by adding the followings line in pubspec.yaml file.

dependencies:
  flutter:
    sdk: flutter
  path_provider: ^2.0.11

Now import the package in your script:

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

Directory tempDir = await getTemporaryDirectory();
tempPath = tempDir.path;
print(tempPath);
//output: /data/user/0/com.example.test/cache

Directory appDocDir = await getApplicationDocumentsDirectory();
appDocPath = appDocDir.path;
print(appDocPath);
//output: /data/user/0/com.example.test/app_flutter

You can use these paths to read and write files in the Flutter app. 

import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';
import 'dart:io';

Future<void> main() async {
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> {
 
  String tempPath = "";
  String appDocPath = "";

  getPaths() async {
      Directory tempDir = await getTemporaryDirectory();
      tempPath = tempDir.path;
      print(tempPath);
      //output: /data/user/0/com.example.test/cache

      Directory appDocDir = await getApplicationDocumentsDirectory();
      appDocPath = appDocDir.path;
      print(appDocPath);
      //output: /data/user/0/com.example.test/app_flutter

      setState(() {
        
      });
  }

  @override
  void initState() {
    getPaths();
    super.initState();
  }
  @override
  Widget build(BuildContext context) { 
    return  Scaffold(
          body: Container(
            padding: EdgeInsets.only(top:80),
            alignment: Alignment.center,
            child: Column(children: [
                  Text("Temp Path: $tempPath"),
                  Text("App Path: $appDocPath")
                 
            ],)
          )
       );
  }
}

In this way, you can get different kinds of paths of the App file system in Flutter.

No any Comments on this Article


Please Wait...