'Cannot create directory in ios flutter
Im using next code:
Directory dir = getApplicationDocumentsDirectory();
Directory newDir = Directory('$dir/test');
await newDir.create();
and added next code in info.plist
<key>LSSupportsOpeningDocumentsInPlace</key>
<true/>
<key>UIFileSharingEnabled</key>
<true/>
<key>UISupportsDocumentBrowser</key>
<true/>
Code executing without error, but directory don't create
Update: The code above is not correct, im using next code
Future<void> testFunc() async {
Directory? saveDirectory;
Directory tempDir = await getApplicationDocumentsDirectory();
saveDirectory = Directory('${tempDir.path}/testDir');
print(saveDirectory.path);
if (await saveDirectory.exists()) {
print('dir exist');
} else {
await saveDirectory.create();
}
}
Update: the path which i get: "/Users/user/Library/Developer/CoreSimulator/Devices/2DDAAD10-03E2-430A-84FF-58859E309C65/data/Containers/Data/Application/A90F1040-391C-4122-9DFE-EE3348310F9A/Documents/testDir"
Solution 1:[1]
As specified by path_provider's example:
Directory appDocDir = await getApplicationDocumentsDirectory();
String appDocPath = appDocDir.path;
The getApplicationDocumentsDirectory is an asynchronous function. This means that if you don't put "await", the code will execute even if the function is not yet finished.
In your sample code, what's happening is that the dir variable hasn't been yet initialized when the code runs over it.
Update your code adding the await, and remember to use and async function.
Directory dir = await getApplicationDocumentsDirectory();
Directory newDir = Directory('$dir/test');
await newDir.create();
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Dani3le_ |
