Skip to content

Clean up api #256

Open
Open
@MarcelGarus

Description

@MarcelGarus

While the flutter_downloader works fine, its API doesn't conform to idiomatic Dart standards in some cases.
That's why I'm currently rewriting the Dart side of the plugin to make it more intuitive.
If implemented, this will be a breaking change and thus require a new major version update.

Especially the DownloadTasks are currently merely a wrapper around some data and not really actionable. I'm looking forward to rewriting the API so that something like the following is possible:

final task = await DownloadTask.create(
  url: 'https://...',
  downloadDirectory: getApplicationDirectory(),
);
task.updates.forEach(print);
task.onCompleted(() => task.openFile());

await Future.delayed(Duration(seconds: 3));
if (!task.isCompleted) {
  print('Download takes longer than three seconds.');
}

await task.waitUntilCompleted();

By using Streams rather than callbacks, it's possible to leverage Dart's strengths of async/await, applying stream transformations and support from the ecosystem. For example, one could easily build a Flutter widget like the following:

// inside a widget's State

DownloadTask task;

void _startDownload() {
  setState(() => task = DownloadTask.create(…));
}

Widget build(BuildContext context) {
  return Column(
    children: <Widget>[
      RaisedButton(onPressed: _startDownload, child: Text('Start download')),
      StreamBuilder<void>(
        stream: task?.updates ?? Stream.empty(),
        builder: (context, _) => Text('Progress: ${task?.progress}'),
      ),
    ],
  );
}

What do you think about this API?

Metadata

Metadata

Assignees

No one assigned

    Labels

    v2Epic feature or breaking change which isn't suitable for v1

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions