我正在尝试为我的应用实现简单的下载管理器。我有文件:
downloader.dart :
List<downloader> downloads = new List<downloader>(); // Used as global variable, to store different downloads.
class downloader{
double progress;
void download() async{
await dio.download(url, path, onProgress: (received, total) { //dio is library to download files
progress = (received / total).toDouble();
});
}
Stream<double> getStream() async* {
yield progress;
}
}
downloadmanager.dart :
import 'package:appname/downloader.dart' as d;
//DEFAULT STATEFUL WIDGET STUFF
class DownloadManager extends StatefulWidget {
@override
_DownloadManagerState createState() => _DownloadManagerState();
}
class _DownloadManagerState extends State<DownloadManager> {
@override
void initState() {
super.initState();
}
@override
Widget build(BuildContext context) {
return new Scaffold(
appBar: new AppBar(title: new Text("Download Manager")),
body:
new StreamBuilder(
stream: d.downloads[0].getStream(), //Hardcoded first element of the array, for testing purproses.
initialData: 0,
builder: (BuildContext context, AsyncSnapshot<double> snapshot) {
return new Text(snapshot.data.toString());
})
);
}
}
当我加载DownloadManager
StatefulWidget时,它会显示加载时的下载进度,但不会更新。如何使DownloadManager
显示当前进度和更新?
注意:加载DownloadManager
时,downloads
列表中已经有元素,并且download()
函数正在执行/完成。
谢谢。