颤抖地下载文件

时间:2018-12-02 13:30:16

标签: download flutter

我制作了一个Flutter应用程序,首先我显示有关pdf文件的所有信息,其中包括文件的url,来自Restfull API的表格,一切正常,但是我想在用户单击该文件时下载该文件,我试试这个:

 onTap: () async {
                                // downloadFile(snapshot.data[index].file);
                                Dio dio = new Dio();
                                Directory dir =
                                    await getApplicationDocumentsDirectory();
                                String path = dir.path;
                                await dio.download(
                                    snapshot.data[index].file, path);
                              },

snapshot.data [index] .file,路径是来自api的正确路径或文件,但是它不起作用,并且在此代码中给我(路径)一个错误

await getApplicationDocumentsDirectory();
String path = dir.path;

this is my error log:

E/flutter (26482): [ERROR:flutter/shell/common/shell.cc(188)] Dart Error: Unhandled exception:
E/flutter (26482): FileSystemException: Cannot open file, path = '/data/data/com.example.mis/app_flutter' (OS Error: Is a
directory, errno = 21)
E/flutter (26482): #0      _File.throwIfError (dart:io/file_impl.dart:647:7)
E/flutter (26482): #1      _File.openSync (dart:io/file_impl.dart:491:5)
E/flutter (26482): #2      Dio.download (package:dio/src/dio.dart:212:20)
E/flutter (26482): <asynchronous suspension>
E/flutter (26482): #3      MaterialsState.build.<anonymous closure>.<anonymous closure>.<anonymous closure>
(package:mis/courses/materials.dart:121:47)
E/flutter (26482): <asynchronous suspension>
E/flutter (26482): #4      _InkResponseState._handleTap (package:flutter/src/material/ink_well.dart:507:14)
E/flutter (26482): #5      _InkResponseState.build.<anonymous closure> (package:flutter/src/material/ink_well.dart:562:30)
E/flutter (26482): #6      GestureRecognizer.invokeCallback (package:flutter/src/gestures/recognizer.dart:102:24)
E/flutter (26482): #7      TapGestureRecognizer._checkUp (package:flutter/src/gestures/tap.dart:242:9)
E/flutter (26482): #8      TapGestureRecognizer.acceptGesture (package:flutter/src/gestures/tap.dart:204:7)
E/flutter (26482): #9      GestureArenaManager.sweep (package:flutter/src/gestures/arena.dart:156:27)
E/flutter (26482): #10     _WidgetsFlutterBinding&BindingBase&GestureBinding.handleEvent
(package:flutter/src/gestures/binding.dart:184:20)
E/flutter (26482): #11     _WidgetsFlutterBinding&BindingBase&GestureBinding.dispatchEvent
(package:flutter/src/gestures/binding.dart:158:22)
E/flutter (26482): #12     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerEvent
(package:flutter/src/gestures/binding.dart:138:7)
E/flutter (26482): #13     _WidgetsFlutterBinding&BindingBase&GestureBinding._flushPointerEventQueue
(package:flutter/src/gestures/binding.dart:101:7)
E/flutter (26482): #14     _WidgetsFlutterBinding&BindingBase&GestureBinding._handlePointerDataPacket
(package:flutter/src/gestures/binding.dart:85:7)
E/flutter (26482): #15     _invoke1 (dart:ui/hooks.dart:159:13)
E/flutter (26482): #16     _dispatchPointerDataPacket (dart:ui/hooks.dart:113:5)

还有其他下载文件的方法吗?甚至只是打开它?非常感谢你

2 个答案:

答案 0 :(得分:1)

在 flutter https://pub.dev/packages/file_support 上使用插件文件支持

Future<File> downloadFile async{
File file =await FileSupport(url:"http://test.com/.mp4");
 return file;
}

答案 1 :(得分:0)

以防万一其他人需要它。
问题是您没有通过文件路径作为参数,而是通过目录。

await dio.download(snapshot.data[index].file, path+'/fileName.pdf');  

那应该可以解决问题。