disk_space_update 0.0.1 copy "disk_space_update: ^0.0.1" to clipboard
disk_space_update: ^0.0.1 copied to clipboard

A small Flutter plugin to display free and total disk space. It is an update to the disk_space plugin.

example/lib/main.dart

import 'dart:async';
import 'dart:io';
import 'package:disk_space_update/disk_space_update.dart';
import 'package:flutter/material.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(const MyApp());

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

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

class MyAppState extends State<MyApp> {
  double _diskSpace = 0;
  Map<Directory, double> _directorySpace = {};

  @override
  void initState() {
    super.initState();
    initDiskSpace();
  }

  Future<void> initDiskSpace() async {
    double diskSpace = 0;

    diskSpace = await DiskSpace.getFreeDiskSpace ?? 0;

    List<Directory> directories;
    Map<Directory, double> directorySpace = {};

    if (Platform.isIOS) {
      directories = [await getApplicationDocumentsDirectory()];
    } else if (Platform.isAndroid) {
      directories =
          await getExternalStorageDirectories(type: StorageDirectory.movies)
              .then(
        (list) async => list ?? [await getApplicationDocumentsDirectory()],
      );
    } else {
      return;
    }

    for (var directory in directories) {
      var space = await DiskSpace.getFreeDiskSpaceForPath(directory.path) ?? 0;
      directorySpace.addEntries([MapEntry(directory, space)]);
    }

    if (!mounted) return;

    setState(() {
      _diskSpace = diskSpace;
      _directorySpace = directorySpace;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Column(
          children: [
            Center(
              child: Text('Space on device (MB): $_diskSpace\n'),
            ),
            Center(
              child: ListView.builder(
                shrinkWrap: true,
                itemBuilder: (context, index) {
                  var key = _directorySpace.keys.elementAt(index);
                  var value = _directorySpace[key];
                  return Text('Space in ${key.path} (MB): $value\n');
                },
                itemCount: _directorySpace.keys.length,
              ),
            ),
          ],
        ),
      ),
    );
  }
}
6
likes
160
points
1.98k
downloads

Publisher

unverified uploader

Weekly Downloads

A small Flutter plugin to display free and total disk space. It is an update to the disk_space plugin.

Homepage

Documentation

API reference

License

MIT (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on disk_space_update