您好,我想使用flutter_downloader包下载文件,我尝试使用示例中的代码 关于文档的问题,我有时会出现黑屏,有时会出现白屏,但我不知道为什么我只尝试使用示例函数中的一个函数,但我却报错并且无法正常工作
import 'dart:isolate';
import 'dart:ui';
import 'package:flutter/material.dart';
import 'dart:async';
import 'package:path_provider/path_provider.dart';
import 'package:flutter_downloader/flutter_downloader.dart';
import 'package:permission_handler/permission_handler.dart';
import 'dart:io';
const debug = true;
class download extends StatefulWidget with WidgetsBindingObserver {
final TargetPlatform platform;
download({Key key, this.title, this.platform}) : super(key: key);
final String title;
@override
_MyHomePageState createState() => new _MyHomePageState();
}
class _MyHomePageState extends State<download> {
String _localPath;
@override
void initState() {
super.initState();
WidgetsFlutterBinding.ensureInitialized();
FlutterDownloader.initialize(
debug: true // optional: set false to disable printing logs to console
);
}
@override
void dispose() {
super.dispose();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(
title: new Text('bonjour'),
),
body:FlatButton(
child:Text('bbbbb'),
onPressed: _requestDownload,
)
);
}
void _requestDownload() async {
await FlutterDownloader.enqueue(
url:'http://barbra-coco.dyndns.org/student/learning_android_studio.pdf' ,
headers: {"auth": "test_for_sql_encoding"},
savedDir: _localPath,
showNotification: true,
openFileFromNotification: true);
}
我有这个例外
[ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: 'package:flutter_downloader/src/downloader.dart': Failed assertion: line 73 pos 12: '_initialized': FlutterDownloader.initialize() must be called first
我在问是否有一个例证或解决方案,或者一个有例证和细节的有效例子谢谢您
答案 0 :(得分:0)
根据错误提示以及使用前必须初始化的文档,
称呼这个
WidgetsFlutterBinding.ensureInitialized();
FlutterDownloader.initialize(
debug: true // optional: set false to disable printing logs to console
);
并在下面的AndroidManifest中添加此代码
<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>
检查documentation了解更多