To use path_provider in your app, follow these steps:
- As usual, the first step of using a library is adding the relevant dependency to the pubspec.yaml file.
- Check the current version of path_provider at https://pub.dev/packages/path_provider.
- Add the dependency to the pubspec.yaml file:
path_provider: ^2.0.1
- At the top of the main.dart file, import path_provider:
import 'package:path_provider/path_provider.dart';
- At the top of the _MyHomePageState class, add the State variables that we will use to update the user interface:
String documentsPath='';
String tempPath='';
- Still in the _MyHomePageState class, add the method for retrieving the temporary and documents directories:
Future getPaths() async {
final docDir = await getApplicationDocumentsDirectory();
final tempDir = await getTemporaryDirectory();
setState(() {
documentsPath = docDir.path;
tempPath = tempDir.path;
});
}
- In the initState...