zoukankan      html  css  js  c++  java
  • flutter 下载文件

    一、Android

    1.引入插件

      permission_handler: 5.0.1+1 #权限请求
      path_provider: 1.6.14 #路径
      flutter_downloader: 1.4.4 #下载

    2. androidmanifest.xml

        <!--网络权限-->
        <uses-permission android:name="android.permission.INTERNET"/>
        <!-- 读写存储权限 -->
        <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
        <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
        <!-- 安装权限 -->
        <uses-permission android:name="android.permission.REQUEST_INSTALL_PACKAGES" />
    
        <application
            android:networkSecurityConfig="@xml/network_security_config"><!--网络安装配置-->
            <!--下载需要的配置-->
            <provider
                android:name="vn.hunghd.flutterdownloader.DownloadedFileProvider"
                android:authorities="${applicationId}.flutter_downloader.provider"
                android:exported="false"
                android:grantUriPermissions="true">
                <meta-data
                    android:name="android.support.FILE_PROVIDER_PATHS"
                    android:resource="@xml/provider_paths"/>
            </provider>
        </application>

    3.

    class _DownTestState extends State<DownTest> {
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(),
          body: Center(
            child: MaterialButton(
              child: Text("data",style: TextStyle(color: Colors.white),),
              color: Colors.blue,
              onPressed: (){
                ()async{
                  //获得临时地址
                  Directory tempDir = await getTemporaryDirectory();
                  String tempPath = tempDir.path;
                  print(tempPath);
                  //请求下载地址
                  final taskId = await FlutterDownloader.enqueue(
                    url: 'http://192.168.18.14:5000/down/1.apk',//下载地址
                    savedDir: tempPath,//保存路径
                    showNotification: true, // show download progress in status bar (for Android)
                    openFileFromNotification: true, // click on notification to open downloaded file (for Android)
                  );
    
                  print("taskId:"+taskId);
                }();
              },
            ),
          ),
        );
      }
    
      void initState() {
    
        //请求权限
        ()async{
          Map<Permission, PermissionStatus> statuses = await [ Permission.storage].request();
        }();
    
        //初始化
        () async{
          WidgetsFlutterBinding.ensureInitialized();
          await FlutterDownloader.initialize(
              debug: true // optional: set false to disable printing logs to console
          );
          //监听回调
          FlutterDownloader.registerCallback(downloadCallback);
        }();
    
      }
    
      //下载回调方法
      static void downloadCallback(String id, DownloadTaskStatus status, int progress) {
        print("id:" + id);
        print("status:" + status.toString());
        print("progress:" + progress.toString());
      }
    }

    二、IOS

    1.开启background mode

     

     2.添加sqlite

     3.

  • 相关阅读:
    http连接池
    消息队列场景简介
    项目中使用到的设计模式
    dubbo 问题整理
    dubbo Filter
    Elastic-Job分布式作业框架
    别被平凡淹没
    spring中@value注解需要注意
    穷人的真相:从7点忙到23点的上班者,跳出穷人圈子唯一可能是.
    ContextLoaderListener类(spring源码解析)
  • 原文地址:https://www.cnblogs.com/buchizaodian/p/13182062.html
Copyright © 2011-2022 走看看