Flutter自动获取存储目录插件condora_automatic_getter_storage_directory的使用

Flutter自动获取存储目录插件condora_automatic_getter_storage_directory的使用

condora_automatic_getter_storage_directory

一个为Flutter提供的包,它提供了一种自动且平台感知的方式来管理跨Web和非Web平台的存储目录,并高效地处理调试和发布环境。

特性

  • 智能的平台特定存储路径,遵循最佳实践:
    • iOS/macOS: 遵循Apple指南的Library目录。
    • Android: 如果可用,优先使用外部存储目录;否则回退到应用支持目录。
    • Windows: 使用AppData/Roaming(可选配置为Local)。
    • Linux: 遵循XDG规范的~/.local/share/[app_name]
    • Web: 可配置的存储目录。
  • 支持调试模式开发,方便访问项目目录存储。
  • 提供全面的错误处理和验证。
  • 跨平台支持(Android、iOS、Windows、macOS、Linux、Web)。

开始使用

pubspec.yaml文件中添加以下依赖项:

dependencies:
  condora_automatic_getter_storage_directory: ^1.0.1

然后运行flutter pub get以安装该包。


使用方法

不使用Hydrated BLoC

以下是一个不使用Hydrated BLoC的简单示例:

import 'package:flutter/material.dart';
import 'package:condora_automatic_getter_storage_directory/condora_automatic_getter_storage_directory.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 获取存储目录
  final storageDir = await condoraAutomaticGetterStorageDirectory(
    webStorageDirectory: Directory('your_web_storage_path'), // Web专用路径
    windowsUseLocalStorage: false, // 是否使用Windows本地存储(默认为Roaming)
  );

  // 打印存储目录路径
  print('Storage directory: ${storageDir.path}');
}

使用Hydrated BLoC

以下是与Hydrated BLoC结合使用的示例:

import 'package:flutter/material.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:condora_automatic_getter_storage_directory/condora_automatic_getter_storage_directory.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化Hydrated BLoC的存储位置
  HydratedBloc.storage = await HydratedStorage.build(
    storageDirectory: await condoraAutomaticGetterStorageDirectory(
      webStorageDirectory: HydratedStorage.webStorageDirectory, // Web专用路径
    ),
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Placeholder(),
      ),
    );
  }
}

存储目录行为

不同平台的存储目录行为如下:

  • iOS/macOS: Library目录(遵循Apple指南)。
  • Android: 如果外部存储可用,则使用外部存储目录;否则回退到应用支持目录。
  • Windows: 默认使用AppData/Roaming,可配置为Local存储。
  • Linux: ~/.local/share/[app_name](遵循XDG规范)。
  • Web: 可配置的自定义目录。
  • 调试模式: 在项目目录中创建一个明确标记的调试文件夹。

其他信息

此包特别适用于以下场景:

  • 开发跨多个平台的Flutter应用。
  • 需要符合平台特定存储位置的应用。
  • 希望在调试时轻松访问存储的开发人员。
  • 使用如hydrated_bloc等需要Web兼容存储的库。
  • 需要遵循平台特定存储指南的开发者。

贡献

欢迎在GitHub仓库提交问题、PR或建议。


许可证

MIT许可证 - 详见LICENSE文件。


示例代码

以下是完整的示例代码,包含主入口和Hydrated BLoC集成:

import 'package:flutter/material.dart';
import 'package:hydrated_bloc/hydrated_bloc.dart';
import 'package:condora_automatic_getter_storage_directory/condora_automatic_getter_storage_directory.dart';

Future<void> main() async {
  WidgetsFlutterBinding.ensureInitialized();

  // 初始化Hydrated BLoC的存储位置
  HydratedBloc.storage = await HydratedStorage.build(
    storageDirectory: await condoraAutomaticGetterStorageDirectory(
      webStorageDirectory: HydratedStorage.webStorageDirectory, // Web专用路径
    ),
  );

  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  [@override](/user/override)
  Widget build(BuildContext context) {
    return const MaterialApp(
      home: Scaffold(
        body: Placeholder(),
      ),
    );
  }
}

更多关于Flutter自动获取存储目录插件condora_automatic_getter_storage_directory的使用的实战教程也可以访问 https://www.itying.com/category-92-b0.html

1 回复

更多关于Flutter自动获取存储目录插件condora_automatic_getter_storage_directory的使用的实战系列教程也可以访问 https://www.itying.com/category-92-b0.html


condora_automatic_getter_storage_directory 是一个 Flutter 插件,用于自动获取设备的存储目录。这个插件可以帮助开发者轻松地获取设备的内部存储或外部存储的路径,以便在应用中进行文件操作。

安装插件

首先,你需要在 pubspec.yaml 文件中添加 condora_automatic_getter_storage_directory 插件的依赖:

dependencies:
  flutter:
    sdk: flutter
  condora_automatic_getter_storage_directory: ^1.0.0  # 请使用最新版本

然后运行 flutter pub get 来安装插件。

使用插件

安装完成后,你可以在你的 Dart 代码中使用这个插件来获取存储目录。

1. 导入插件

import 'package:condora_automatic_getter_storage_directory/condora_automatic_getter_storage_directory.dart';

2. 获取存储目录

你可以使用 CondoraAutomaticGetterStorageDirectory 类来获取存储目录。以下是一个简单的示例:

void getStorageDirectory() async {
  try {
    // 获取内部存储目录
    String internalStoragePath = await CondoraAutomaticGetterStorageDirectory.getInternalStorageDirectory();
    print('Internal Storage Path: $internalStoragePath');

    // 获取外部存储目录
    String externalStoragePath = await CondoraAutomaticGetterStorageDirectory.getExternalStorageDirectory();
    print('External Storage Path: $externalStoragePath');
  } catch (e) {
    print('Error: $e');
  }
}

3. 在应用中使用

你可以在应用的任何地方调用 getStorageDirectory 方法来获取存储目录。例如,在 initState 中调用:

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();
    getStorageDirectory();
  }

  void getStorageDirectory() async {
    try {
      String internalStoragePath = await CondoraAutomaticGetterStorageDirectory.getInternalStorageDirectory();
      print('Internal Storage Path: $internalStoragePath');

      String externalStoragePath = await CondoraAutomaticGetterStorageDirectory.getExternalStorageDirectory();
      print('External Storage Path: $externalStoragePath');
    } catch (e) {
      print('Error: $e');
    }
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: Text('Storage Directory Example'),
        ),
        body: Center(
          child: Text('Check the console for storage paths'),
        ),
      ),
    );
  }
}

注意事项

  1. 权限:在 Android 上,访问外部存储可能需要 READ_EXTERNAL_STORAGEWRITE_EXTERNAL_STORAGE 权限。确保在 AndroidManifest.xml 中添加这些权限:

    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
回到顶部