disk_space_2 1.0.2 copy "disk_space_2: ^1.0.2" to clipboard
disk_space_2: ^1.0.2 copied to clipboard

A Flutter package that provides an easy way to get disk space information on Android and iOS devices.

example/lib/main.dart

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

import 'package:flutter/services.dart';
import 'package:disk_space/disk_space.dart';

void main() {
  runApp(const MyApp());
}

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

  @override
  State<MyApp> createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  String _platformVersion = 'Unknown';
  double _totalDiskSpace = 0.0;
  double _freeDiskSpace = 0.0;
  // double _freeDiskSpaceForPath = 0.0;

  final _diskSpacePlugin = DiskSpace();

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

  // Platform messages are asynchronous, so we initialize in an async method.
  Future<void> initPlatformState() async {
    String platformVersion;
    double totalDiskSpace;
    double freeDiskSpace;
    // double freeDiskSpaceForPath;

    // Platform messages may fail, so we use a try/catch PlatformException.
    // We also handle the message potentially returning null.
    try {
      platformVersion = await _diskSpacePlugin.getPlatformVersion() ??
          'Unknown platform version';
    } on PlatformException {
      platformVersion = 'Failed to get platform version.';
    }

    totalDiskSpace = await DiskSpace.getTotalDiskSpace ?? 0.0;
    freeDiskSpace = await DiskSpace.getFreeDiskSpace ?? 0.0;

    // try {
    //   freeDiskSpace = await DiskSpace.getFreeDiskSpace;
    // } on PlatformException {
    //   print("FAILED TO GET TOTAL DISK SPACE");
    // }

    // If the widget was removed from the tree while the asynchronous platform
    // message was in flight, we want to discard the reply rather than calling
    // setState to update our non-existent appearance.
    if (!mounted) return;

    setState(() {
      _platformVersion = platformVersion;
      _totalDiskSpace = totalDiskSpace;
      _freeDiskSpace = freeDiskSpace;
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Scaffold(
        appBar: AppBar(
          title: const Text('Plugin example app'),
        ),
        body: Center(
          child: Column(
            children: [
              Text('Running on: $_platformVersion'),
              Text('Total disk space: $_totalDiskSpace mb'),
              Text('Free disk space: $_freeDiskSpace mb'),
              Text('Free disk space: ${_freeDiskSpace / 1000} GB'),
            ],
          ),
        ),
      ),
    );
  }
}
5
likes
0
pub points
89%
popularity

Publisher

unverified uploader

A Flutter package that provides an easy way to get disk space information on Android and iOS devices.

Repository (GitHub)
View/report issues

License

unknown (license)

Dependencies

flutter, plugin_platform_interface

More

Packages that depend on disk_space_2