Saya sedang mengerjakan proyek dalam flutter dan saya perlu menerapkan pengunduhan video dari fitur server. Saya sedang mempertimbangkan untuk menggunakan perpustakaan Dio dan menyimpan video yang diunduh ke getApplicationDocumentsDirectory() tetapi saya belum menemukan contoh yang saya inginkan untuk mencapainya, saya mencoba beberapa contoh dan mencoba memodifikasinya tetapi tidak berhasil. Adakah yang punya ide tentang cara mengelolanya? Terima kasih.

1
Sajjad Emad 19 Maret 2020, 20:11

1 menjawab

Jawaban Terbaik

Anda dapat menyalin tempel menjalankan kode lengkap di bawah ini

Cuplikan kode

Future<void> downloadFile() async {
    Dio dio = Dio();

    try {
      var dir = await getApplicationDocumentsDirectory();
      print("path ${dir.path}");
      await dio.download(imgUrl, "${dir.path}/demo.mp4",
          onReceiveProgress: (rec, total) {
        print("Rec: $rec , Total: $total");

        setState(() {
          downloading = true;
          progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
        });
      });
    } catch (e) {
      print(e);
    }

    setState(() {
      downloading = false;
      progressString = "Completed";
    });
    print("Download completed");
  }

Demo kerja

enter image description here

Keluaran

I/flutter (18244): path /data/user/0/yourdoamin.yourproject/app_flutter
I/flutter (18244): Rec: 2124 , Total: 2429896
I/flutter (18244): Rec: 4883 , Total: 2429896
...
I/flutter (18244): Rec: 2429896 , Total: 2429896
I/flutter (18244): Download completed

Kode lengkap

import 'dart:async';

import 'package:flutter/material.dart';
import 'package:dio/dio.dart';
import 'package:path_provider/path_provider.dart';

void main() => runApp(MaterialApp(
      home: MyApp(),
      debugShowCheckedModeBanner: false,
    ));

class MyApp extends StatefulWidget {
  @override
  MyAppState createState() {
    return new MyAppState();
  }
}

class MyAppState extends State<MyApp> {
  final imgUrl = "https://flutter.github.io/assets-for-api-docs/assets/videos/butterfly.mp4";
  bool downloading = false;
  var progressString = "";

  @override
  void initState() {
    super.initState();

    downloadFile();
  }

  Future<void> downloadFile() async {
    Dio dio = Dio();

    try {
      var dir = await getApplicationDocumentsDirectory();
      print("path ${dir.path}");
      await dio.download(imgUrl, "${dir.path}/demo.mp4",
          onReceiveProgress: (rec, total) {
        print("Rec: $rec , Total: $total");

        setState(() {
          downloading = true;
          progressString = ((rec / total) * 100).toStringAsFixed(0) + "%";
        });
      });
    } catch (e) {
      print(e);
    }

    setState(() {
      downloading = false;
      progressString = "Completed";
    });
    print("Download completed");
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("AppBar"),
      ),
      body: Center(
        child: downloading
            ? Container(
                height: 120.0,
                width: 200.0,
                child: Card(
                  color: Colors.black,
                  child: Column(
                    mainAxisAlignment: MainAxisAlignment.center,
                    children: <Widget>[
                      CircularProgressIndicator(),
                      SizedBox(
                        height: 20.0,
                      ),
                      Text(
                        "Downloading File: $progressString",
                        style: TextStyle(
                          color: Colors.white,
                        ),
                      )
                    ],
                  ),
                ),
              )
            : Text("No Data"),
      ),
    );
  }
}
1
chunhunghan 20 Maret 2020, 02:21