一統天下 flutter - 存儲: path_provider - 用於獲取不同平臺的本地存儲的路徑

源碼 https://github.com/webabcd/flutter_demo
作者 webabcd

一統天下 flutter - 存儲: path_provider - 用於獲取不同平臺的本地存儲的路徑

示例如下:

lib\storage\path_provider.dart

/*
 * path_provider - 用於獲取不同平臺的本地存儲的路徑
 *
 * 在 pubspec.yaml 中做如下配置,然後 flutter pub get
 * dev_dependencies:
 *   flutter_test:
 *     sdk: flutter
 *   path_provider: ^2.0.0
 *
 *
 * 常用的目錄如下:
 * getTemporaryDirectory() - ios 的 NSCachesDirectory; android 的 getCacheDir()
 * getApplicationSupportDirectory() - ios 的 NSApplicationSupportDirectory; android 的 getFilesDir()
 * getApplicationDocumentsDirectory() - ios 的 NSDocumentDirectory; android 的 getDataDir() + "/app_flutter/"
 *
 * 不常用的目錄如下:
 * getLibraryDirectory() - 獲取 ios 的 Library 目錄
 * getExternalStorageDirectory(), getExternalStorageDirectories(), getExternalCacheDirectories() - 獲取 android 的外部存儲相關的目錄
 * getDownloadsDirectory() - 獲取桌面程序的下載目錄
 */

import 'dart:io';

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

import '../helper.dart';

class PathProviderDemo extends StatefulWidget {
  const PathProviderDemo({Key? key}) : super(key: key);

  @override
  _PathProviderDemoState createState() => _PathProviderDemoState();
}

class _PathProviderDemoState extends State<PathProviderDemo> {
  Future<Directory?>? _tempDirectory;
  Future<Directory?>? _appSupportDirectory;
  Future<Directory?>? _appDocumentsDirectory;

  Widget _buildDirectory(BuildContext context, AsyncSnapshot<Directory?> snapshot) {
    var result = "";
    if (snapshot.connectionState == ConnectionState.done) {
      if (snapshot.hasError) {
        result = 'error: ${snapshot.error}';
      } else if (snapshot.hasData) {
        result = 'path: ${snapshot.data!.path}';
      } else {
        result = 'path unavailable';
      }
    }
    return MyTextSmall(result);
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(title: const Text("title"),),
      backgroundColor: Colors.orange,
      body: Center(
        child: ListView(
          children: [
            Column(
              children: [
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      /// ios 地址類似 xxx/Library/Caches
                      /// android 地址類似 /data/user/0/packagename/cache
                      _tempDirectory = getTemporaryDirectory();
                    });
                  },
                  child: const Text('getTemporaryDirectory()',),
                ),
                FutureBuilder<Directory?>(
                  future: _tempDirectory,
                  builder: _buildDirectory,
                ),
              ],
            ),
            const SizedBox(height: 16,),
            Column(
              children: [
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      /// ios 地址類似 xxx/Library/Application Support
                      /// android 地址類似 /data/user/0/packagename/files
                      _appSupportDirectory = getApplicationSupportDirectory();
                    });
                  },
                  child: const Text('getApplicationSupportDirectory()',),
                ),
                FutureBuilder<Directory?>(
                  future: _appSupportDirectory,
                  builder: _buildDirectory,
                ),
              ],
            ),
            const SizedBox(height: 16,),
            Column(
              children: [
                ElevatedButton(
                  onPressed: () {
                    setState(() {
                      /// ios 地址類似 xxx/Documents
                      /// android 地址類似 /data/user/0/packagename/app_flutter
                      _appDocumentsDirectory = getApplicationDocumentsDirectory();
                    });
                  },
                  child: const Text('getApplicationDocumentsDirectory()',),
                ),
                FutureBuilder<Directory?>(
                  future: _appDocumentsDirectory,
                  builder: _buildDirectory,
                ),
              ],
            ),
          ],
        ),
      ),
    );
  }
}

源碼 https://github.com/webabcd/flutter_demo
作者 webabcd

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章