download_manager 1.0.1+2

  • Readme
  • Changelog
  • Example
  • Installing
  • 85

download_manager #

Build Status

A package for automatically downloading and storing files

Getting Started #

Creating a DownloadableFile #

The first step is to create a downloadable file. The simplest way to do this is using a SimpleDownloadableFile

E.g. you can create one with a function (returning something can be written to file)

  File testFile = File("test_file.txt");
  var downloadFile = DownloadableFileBasic(() => "Test string", testFile);

You can also, optionally, set an expiry date time to your DownloadableFileBasic class. The purpose of this is to have a file which is only downloaded if the expiry date on the file is newer than the one you've already downloaded

Downloading a file #

Insert your downloadable file into the DownloadManager

DownloadManager.instance().add(DownloadableFileBasic(() => "Test string", testBFile));

Results in the stream

expectLater(DownloadManager.instance().fileStream, emits(testBFile));

Get notifications #

There are two streams (more in development) that you can subscribe to.

The first will return a stream which has files in fired one at a time as they are downloaded.

DownloadManager.instance().fileStream

The second will return a list of all files downloaded. Note: files that are already downloaded will also be added to this list

DownloadManager.instance().allFiles

You can also clear all files. You will be notified via allFiles stream with a new empty list that this has happened

DownloadManager.instance().clear();

[0.0.1] - 17/06/2019 #

  • Initial release

[1.0.0] - 18/06/2019 #

  • Addressing dart pub issues

[1.0.1] - 18/06/2019 #

  • Adding example
  • Adding clear
  • Adding ability to monitor all files downloaded in a list

example/lib/main.dart

import 'dart:async';
import 'dart:io';

import 'package:example/util.dart';
import 'package:flutter/material.dart';
import 'package:download_manager/download_manager.dart';
import 'package:http/http.dart' as http;
import 'package:rxdart/rxdart.dart';
import 'package:auto_size_text/auto_size_text.dart';

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

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Download manager - Demo app',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: Scaffold(
        body: SafeArea(
          child: DownloadListPage(
              files: ["http://ipv4.download.thinkbroadband.com/100MB.zip"]),
        ),
      ),
    );
  }
}

class DownloadedFile {
  final File file;
  final bool downloaded;

  DownloadedFile(this.file, this.downloaded);
}

class DownloadListPage extends StatefulWidget {
  /// URL of files to download
  final List<String> files;

  const DownloadListPage({Key key, this.files}) : super(key: key);

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

class _DownloadListPageState extends State<DownloadListPage> {
  StreamController<List<DownloadableFileBasic>> downloadableFiles =
      StreamController.broadcast();
  List<DownloadableFileBasic> cache = List();

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<List<DownloadedFile>>(
        stream: Observable.combineLatest2(
            downloadableFiles.stream, DownloadManager.instance().allFiles,
            (List<DownloadableFile> downloadable, List<File> downloaded) {
          List<DownloadedFile> files = List();
          downloadable.forEach((f) => files.add(DownloadedFile(
              f.destinationFile,
              downloaded.where((d) => d == f.destinationFile).isNotEmpty)));
          return files;
        }),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return Center(child: CircularProgressIndicator());
          }
          return Center(
            child: Container(
              margin: const EdgeInsets.all(8.0),
              child: ListView(
                  children: snapshot.data
                      .map((x) => Row(
                            children: <Widget>[
                              Container(
                                  margin: const EdgeInsets.all(8.0),
                                  width: MediaQuery.of(context).size.width / 2 -
                                      30,
                                  child: AutoSizeText(x.file.absolute.path,
                                      maxLines: 2)),
                              Container(
                                margin: const EdgeInsets.all(8.0),
                                width:
                                    MediaQuery.of(context).size.width / 2 - 30,
                                child: AutoSizeText(
                                    x.downloaded
                                        ? "Downloaded"
                                        : "Not downloaded",
                                    maxLines: 2),
                              )
                            ],
                          ))
                      .toList()
                        ..add(Row(
                          children: [
                            FlatButton(
                              onPressed: () {
                                DownloadManager.instance().clear();
                              },
                              child: Text("Clear"),
                            ),
                            FlatButton(
                              onPressed: () {
                                cache.forEach(
                                    (f) => DownloadManager.instance().add(f));
                              },
                              child: Text("Download"),
                            )
                          ],
                        ))),
            ),
          );
        });
  }

  @override
  void initState() {
    super.initState();
    widget.files.forEach((f) async {
      var downloadable = DownloadableFileBasic(
          () => _download(f), await UrlToFilename.file(f));

      cache.add(downloadable);
      DownloadManager.instance().add(downloadable);
      downloadableFiles.add(cache);
    });
  }

  Future<String> _download(String url) async {
    return (await http.get(url)).body;
  }

  @override
  void dispose() {
    super.dispose();
    DownloadManager.instance().dispose();
  }
}

Use this package as a library

1. Depend on it

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


dependencies:
  download_manager: ^1.0.1+2

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:download_manager/download_manager.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
70
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
85
Learn more about scoring.

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

  • Dart: 2.8.4
  • pana: 0.13.15
  • Flutter: 1.17.5

Analysis suggestions

Package does not support Flutter platform Android

Because:

  • package:download_manager/download_manager.dart that imports:
  • package:download_manager/src/store.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform Web

Because:

  • package:download_manager/download_manager.dart that imports:
  • package:download_manager/src/store.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform Windows

Because:

  • package:download_manager/download_manager.dart that imports:
  • package:download_manager/src/store.dart that imports:
  • package:shared_preferences/shared_preferences.dart that declares support for platforms: Android, iOS, Linux, macOS, Web

Package does not support Flutter platform iOS

Because:

  • package:download_manager/download_manager.dart that imports:
  • package:download_manager/src/store.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package does not support Flutter platform macOS

Because:

  • package:download_manager/download_manager.dart that imports:
  • package:download_manager/src/store.dart that imports:
  • package:shared_preferences/shared_preferences.dart that imports:
  • package:shared_preferences_linux/shared_preferences_linux.dart that declares support for platforms: Linux

Package not compatible with SDK dart

Because:

  • download_manager that is a package requiring null.

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.1.0 <3.0.0
flutter 0.0.0
shared_preferences >=0.5.3 <0.6.0 0.5.8
Transitive dependencies
collection 1.14.12 1.14.13
file 5.2.1
flutter_web_plugins 0.0.0
intl 0.16.1
meta 1.1.8 1.2.2
path 1.7.0
path_provider_linux 0.0.1+2
path_provider_platform_interface 1.0.2
platform 2.2.1
plugin_platform_interface 1.0.2
process 3.0.13
shared_preferences_linux 0.0.2+1
shared_preferences_macos 0.0.1+10
shared_preferences_platform_interface 1.0.4
shared_preferences_web 0.1.2+7
sky_engine 0.0.99
typed_data 1.1.6 1.2.0
vector_math 2.0.8 2.1.0-nullsafety
xdg_directories 0.1.0
Dev dependencies
flutter_test
mockito ^4.0.0