Ammar Al-Wazzan
Ammar Al-Wazzan

Reputation: 115

Flutter | Dio Package ... downloading Large Files in Background

I am using flutter with dio package to download files like powerpoint and videos ...etc

what i want to ask is how to download large file in background

try {
    await dio.download(
        fileURL, '$dir/$fileName.pptx',
        onReceiveProgress: showDownloadProgress,
        deleteOnError: true);
    print("BBBB");

    openDownloadedFile = '$dir/$fileName.pptx';
    print("CCCC");
  } on DioError catch(e) {
    print("11");

    final file = File('$dir/$fileName.pptx');
    file.deleteSync(recursive: true);
    if(e.response != null) {
      print("22");

      print(e.response.data);
      print(e.response.headers);
      print(e.response.request);
    } else{

      // Something happened in setting up or sending the request that triggered an Error
      print(e.request);
      print(e.message);
    }
  }

Best Regard

Upvotes: 2

Views: 6240

Answers (2)

nycynik
nycynik

Reputation: 7541

Since this question has been asked, there is official support in Flutter for Background Tasks. You may want to check that out now.

Docs Link: https://docs.flutter.dev/development/packages-and-plugins/background-processes

Example Link: https://flutteragency.com/schedule-background-tasks/

Complete Example (from the docs)

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

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

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

  @override
  Widget build(BuildContext context) {
    const title = 'WebSocket Demo';
    return const MaterialApp(
      title: title,
      home: MyHomePage(
        title: title,
      ),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({
    super.key,
    required this.title,
  });

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  final TextEditingController _controller = TextEditingController();
  final _channel = WebSocketChannel.connect(
    Uri.parse('wss://echo.websocket.events'),
  );

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Padding(
        padding: const EdgeInsets.all(20.0),
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.start,
          children: [
            Form(
              child: TextFormField(
                controller: _controller,
                decoration: const InputDecoration(labelText: 'Send a message'),
              ),
            ),
            const SizedBox(height: 24),
            StreamBuilder(
              stream: _channel.stream,
              builder: (context, snapshot) {
                return Text(snapshot.hasData ? '${snapshot.data}' : '');
              },
            )
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _sendMessage,
        tooltip: 'Send message',
        child: const Icon(Icons.send),
      ), // This trailing comma makes auto-formatting nicer for build methods.
    );
  }

  void _sendMessage() {
    if (_controller.text.isNotEmpty) {
      _channel.sink.add(_controller.text);
    }
  }

  @override
  void dispose() {
    _channel.sink.close();
    _controller.dispose();
    super.dispose();
  }
}

Upvotes: 1

jinyus
jinyus

Reputation: 566

I would suggest that you use the Flutter Downloader plugin to download large files as it uses the native download manager. Dio is more suited for downloading small files while the app is open.

Upvotes: 1

Related Questions