Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support sound null safety #3

Open
joonashak opened this issue Sep 22, 2022 · 1 comment
Open

Support sound null safety #3

joonashak opened this issue Sep 22, 2022 · 1 comment

Comments

@joonashak
Copy link

I get the following error when trying to use dart_app_data in a project:

Error: Cannot run with sound null safety, because the following dependencies
don't support null safety:

 - package:dart_app_data

Is it possible to upgrade this package to use sound null safety rather than having to go through the hassle of using mixed versions as described here?

PS. I'm a beginner with Dart but if you give me some pointers as to where to start, I'll be happy to try and put together a PR to solve this issue.

@cyberpwnn
Copy link

@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);
    });
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants