file_access 1.0.3

  • Readme
  • Changelog
  • Example
  • Installing
  • 86

file_access #

An abstract way to handle files on iOS, Android, Desktop and Web!

Online Demo: https://rodydavis.github.io/file_access/

Installing:

dependencies:
  flutter:
    sdk: flutter
  file_access:
    git: https://github.com/rodydavis/file_access

Setup #

Web #

Add the following line to your body to make it work in Safari:

<input type="file" style="visibility:hidden;" id="file-picker" />

IOS #

https://github.com/miguelpruivo/flutter_file_picker/wiki/Setup#ios

Getting Started #

Pick a file:

final _file = await openFile();

Select multiple files:

final _files = await openFiles();

Pick an image:

final _file = await pickImage();

Pick a video:

final _file = await pickVideo();

Once you have the FileX type you can read the data:

final _bytes = await _file.readAsBytes();
final _string = await _file.readAsString();

Creating a new file:

final _file = FileX('path/to/file/file.txt');
await _file.writeAsString('My New Data!');
final _output = await _file.readAsString();
print(_output); // 'My New Data!'

1.0.3 #

  • Fix for Safari

1.0.2 #

  • Added FileX

example/lib/main.dart

import 'dart:typed_data';

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

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

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData.light(),
      darkTheme: ThemeData.dark(),
      home: MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  MyHomePage({Key key, this.title}) : super(key: key);

  final String title;

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

class _MyHomePageState extends State<MyHomePage> {
  FileX _file;
  bool isImage = false;
  bool _loading = false;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: _loading
            ? CircularProgressIndicator()
            : _file == null ? Text('No File Selected') : _buildFile(_file),
      ),
      persistentFooterButtons: <Widget>[
        IconButton(
          onPressed: () async {
            if (mounted)
              setState(() {
                isImage = false;
                _file = null;
                _loading = true;
              });
            final _newFile = await openFile();
            if (mounted)
              setState(() {
                _file = _newFile;
                _loading = false;
              });
          },
          tooltip: 'Select File',
          icon: Icon(Icons.file_upload),
        ),
        IconButton(
          onPressed: () async {
            if (mounted)
              setState(() {
                _file = null;
                isImage = true;
                _loading = true;
              });
            final _newFile = await pickImage();
            if (mounted)
              setState(() {
                _file = _newFile;
                _loading = false;
              });
          },
          tooltip: 'Select Image',
          icon: Icon(Icons.image),
        ),
        IconButton(
          onPressed: () async {
            if (mounted)
              setState(() {
                _file = null;
                isImage = false;
                _loading = true;
              });
            final _newFile = await pickVideo();
            if (mounted)
              setState(() {
                _file = _newFile;
                _loading = false;
              });
          },
          tooltip: 'Select Video',
          icon: Icon(Icons.video_library),
        ),
      ],
    );
  }

  Widget _buildFile(FileX file) {
    return FutureBuilder<List<int>>(
        future: file.readAsBytes(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Text(file.path);
          }
          if (isImage) {
            final _bytes = Uint8List.fromList(snapshot.data);
            return Image.memory(
              _bytes,
              fit: BoxFit.contain,
            );
          }
          return Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Padding(
                padding: const EdgeInsets.all(20.0),
                child: Text(file.path),
              ),
              Padding(
                padding: const EdgeInsets.all(20.0),
                child: Text(
                  'Bytes: ${snapshot.data}',
                  maxLines: 3,
                  overflow: TextOverflow.ellipsis,
                ),
              ),
            ],
          );
        });
  }
}

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  file_access: ^1.0.3

2. Install it

You can install packages from the command line:

with Flutter:


$ flutter pub get

Alternatively, your editor might support flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:file_access/file_access.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
73
Health:
Code health derived from static analysis. [more]
98
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
86
Learn more about scoring.

We analyzed this package on Jul 11, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.4
  • pana: 0.13.14
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform linux

Because:

  • package:file_access/file_access.dart that imports:
  • package:file_access/src/pick_file/pick_file.dart that imports:
  • package:file_access/src/pick_file/io.dart that imports:
  • package:image_picker/image_picker.dart that declares support for platforms: android, ios

Package does not support Flutter platform macos

Because:

  • package:file_access/file_access.dart that imports:
  • package:file_access/src/pick_file/pick_file.dart that imports:
  • package:file_access/src/pick_file/io.dart that imports:
  • package:image_picker/image_picker.dart that declares support for platforms: android, ios

Package does not support Flutter platform windows

Because:

  • package:file_access/file_access.dart that imports:
  • package:file_access/src/pick_file/pick_file.dart that imports:
  • package:file_access/src/pick_file/io.dart that imports:
  • package:image_picker/image_picker.dart that declares support for platforms: android, ios

Package not compatible with SDK dart

Because:

  • file_access that is a package requiring null.

Health issues and suggestions

Document public APIs. (-1 points)

12 out of 12 API elements have no dartdoc comment.Providing good documentation for libraries, classes, functions, and other API elements improves code readability and helps developers find and use your API.

Fix lib/src/pick_file/io.dart. (-1 points)

Analysis of lib/src/pick_file/io.dart reported 2 hints:

line 24 col 39: 'pickImage' is deprecated and shouldn't be used. Use imagePicker.getImage() method instead..

line 40 col 39: 'pickVideo' is deprecated and shouldn't be used. Use imagePicker.getVideo() method instead..

Format lib/file_access.dart.

Run flutter format to format lib/file_access.dart.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
file_picker ^1.5.0+2 1.12.0
flutter 0.0.0
image_picker ^0.6.3+4 0.6.7+4
Transitive dependencies
charcode 1.1.3
collection 1.14.12 1.14.13
file_picker_platform_interface 1.2.0
flutter_plugin_android_lifecycle 1.0.8
http 0.12.1
http_parser 3.1.4
image_picker_platform_interface 1.1.0
meta 1.1.8 1.2.2
path 1.7.0
pedantic 1.9.0 1.9.2
plugin_platform_interface 1.0.2
sky_engine 0.0.99
source_span 1.7.0
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
Dev dependencies
flutter_test