How to Implement Download Option in Flutter
How to Implement Download Option in Flutter

Downloading files in flutter is must required feature to be added into any flutter where their are document or any kind of files that the user want to download & store if for later usage.

Media download in flutter is seamless bring media content to your flutter application.

Flutter Downloader – How to implement files downloading in flutter application

To add download feature in flutter application we will make use of a package called flutter_media_downloader, This package has capability of downloading any kind of files like, Images, Videos, Document like pdf, docx etc.

Video Tutorial

Download Files in Flutter

1. Create a Flutter Project or Open Existing

Create a new Flutter Project or Open any Existing Flutter in your favorite IDE VSCode or Android Studio. I use android studio for building flutter application.


2. Add flutter_media_downloader package.

In your flutter project open pubspec.yaml files and under dependencies section add the flutter media downloader package as shown below

#screenshot

and then run flutter pub get to download the package as external libraries.


3. Add permission for Android & IOS

As the files or media get downloaded from Internet We need Internet Permission & to store the downloaded files locally in mobile devices we need Storage Permission.

Android: Open AndroidManifest.xml file and add below lines

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.POST_NOTIFICATIONS"/>

iOS: Open info.plist files and add below lines

<dict>
    <key>NSPhotoLibraryUsageDescription</key>
    <string>We need access to Photo library</string>
</dict>

4. How to Implement flutter media downloader

Complete Source Code

main.dart

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

void main() async{
  WidgetsFlutterBinding.ensureInitialized();
  runApp(const MyApp());
}

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

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      debugShowCheckedModeBanner: false,
      theme: ThemeData(

        colorScheme: ColorScheme.fromSeed(seedColor: Colors.deepPurple),
        useMaterial3: true,
      ),
      home: const MyHomePage(),
    );
  }
}

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

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

class _MyHomePageState extends State<MyHomePage> {
  final _flutterDownload = MediaDownload();
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Center(
        child: ElevatedButton(
          onPressed: () async{
            _flutterDownload.downloadMedia(context, 'https://32net.id/bukaheula/share/otQ9czP4vdLTyiJdmlAXaNK76xKtu38ykwnTm8q0.pdf');
          }, child: Text('Download'),

        ),
      ),
    );
  }
}