Comments (1)
@joonashak Its fairly simple, add path: ^1.8.2
to your pubspec dependencies then put this into a single file called app_data.dart
somewhere in your project with the following contents (its easier to copy then re-publishing / nagging devs).
Then just import it from your own files!
import 'dart:io' show Directory, FileSystemEntity, Platform;
import 'package:path/path.dart' as path_lib;
/// Static helper class for determining platform's app data path.
///
/// Does not require [AppData] to work, can be standalone.
/// Paths for MacOS and Linux were choosen based on popular
/// StackOverflow answers. Submit a PR if you believe these are
/// wrong.
class Locator {
static String getPlatformSpecificCachePath() {
var os = Platform.operatingSystem;
switch (os) {
case 'windows':
return _verify(_findWindows());
case 'linux':
return _verify(_findLinux());
case 'macos':
return _verify(_findMacOS());
case 'android':
case 'ios':
throw LocatorException(
'App caches are not supported for mobile devices');
}
throw LocatorException(
'Platform-specific cache path for platform "$os" was not found');
}
static String _verify(String path) {
if (Directory(path).existsSync()) {
return path;
}
throw LocatorException(
'The user application path for this platform ("$path") does not exist on this system');
}
static String _findWindows() {
return path_lib.join(
Platform.environment['UserProfile']!, 'AppData', 'Roaming');
}
static String _findMacOS() {
return path_lib.join(
Platform.environment['HOME']!, 'Library', 'Application Support');
}
static String _findLinux() {
return path_lib.join('home', Platform.environment['HOME']);
}
}
class LocatorException implements Exception {
final String message;
const LocatorException(this.message);
@override
String toString() => 'LocatorException: $message';
}
/// Represents a custom folder in the platform's AppData folder equivalence.
///
/// Use [name] to define the name of the folder. It will automatically be created
/// if it does not exist already. Access the path of the cache using [path] or
/// directly access the directory by using [directory].
class AppData {
final String name;
String get path => _path;
Directory get directory => _directory;
late String _path;
late Directory _directory;
AppData.findOrCreate(this.name) {
_findOrCreate();
}
void _findOrCreate() {
final cachePath = Locator.getPlatformSpecificCachePath();
_path = path_lib.join(cachePath, name);
_directory = Directory(_path);
if (!_directory.existsSync()) {
_directory.createSync();
}
}
void delete() {
_directory.delete(recursive: true);
}
void clear() {
_directory.list(recursive: true).listen((FileSystemEntity entity) {
entity.delete(recursive: true);
});
}
}
from dart_app_data.
Related Issues (2)
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from dart_app_data.