在Dart中执行下一个代码之前,如何等待代码执行?
我正在在Dart中执行下一个代码之前,如何等待代码执行?,dart,flutter,Dart,Flutter,我正在flatter和Dart中开发墙纸应用程序。目前我正在使用“设置墙纸”按钮,我需要检查墙纸文件是否存在,如果需要下载,然后更改墙纸 这就是我现在所拥有的,我认为我做得很好,请注意,我是Android Java开发人员,只有大约6个月的经验,因此我在Dart方面的基础知识也不多,但不是很好 下载壁纸功能 static Future<int> downloadWallpaperFile(int wallpaperID, {String path}) async { ///Pre
flatter
和Dart
中开发墙纸应用程序。目前我正在使用“设置墙纸”按钮,我需要检查墙纸文件是否存在,如果需要下载,然后更改墙纸
这就是我现在所拥有的,我认为我做得很好,请注意,我是Android Java开发人员,只有大约6个月的经验,因此我在Dart
方面的基础知识也不多,但不是很好
下载壁纸功能
static Future<int> downloadWallpaperFile(int wallpaperID,
{String path}) async {
///Prepare a url for downloading the wallpaper using the getWallpaperURL method and passing in fullSizedWallpaper string constant
String url = getWallpaperURL(WallpaperSize.fullWallpaper, wallpaperID);
///Log output
print('CallingDownloadWallpaper : ' + url);
///Visual Feedback
wallpaperDetailsPageScaffoldGlobalKey.currentState.showSnackBar(
new SnackBar(content: new Text('Starting Wallpaper Download...')));
///Start downloading the wallpaper file from the url
var data = http.readBytes(url);
///After download is completed
data.then((buffer) async {
///If filePath is not passed in as parameter
if (path == null) {
///Use getPathForWallpaperFile to get a path for a wallpaper file
path = await getPathForWallpaperFile(url);
}
///Create a new file at the path, the path also includes the name of the file which is the id of the wallpaper
File newFile = new File(path);
///Get write access to the newly created wallpaper file
RandomAccessFile rf = newFile.openSync(mode: FileMode.write);
///Write the downloaded data to the file synchronously
rf.writeFromSync(buffer);
///Save the file to the disk synchronously
rf.flushSync();
///Close access to file synchronously
rf.closeSync();
///Log output
print('DownloadWallpaperResult : Complete');
///Visual Feedback
wallpaperDetailsPageScaffoldGlobalKey.currentState.showSnackBar(
new SnackBar(content: new Text('Wallpaper Download Complete')));
});
return 0;
}
static setWallpaper(int wallpaperID) async {
///Prepare variables for setting wallpaper and download the wallpaper as well (if needed)
String url = getWallpaperURL(WallpaperSize.fullWallpaper, wallpaperID);
String path = await getPathForWallpaperFile(url);
bool fileExists = checkIfFileExists(path);
///If wallpaper file does not exist then download it
if (fileExists == false) {
///Download wallpaper then change wallpaper
await downloadWallpaperFile(wallpaperID, path: path).then((result) {
///Check if download was successful
if (result == 0) {
///Change wallpaper
AndroidInterface.setWallpaper(path);
}
});
} else {
///Wallpaper already downloaded
///Change wallpaper
AndroidInterface.setWallpaper(path);
}
}
问题是您使用的是
then
,这是非阻塞的(基本上是使用Future
s而不使用wait
)的旧方法)
相反,请使用wait
:
static Future<int> downloadWallpaperFile(int wallpaperID, {String path}) async {
// ...
//Start downloading the wallpaper file from the url
final buffer = await http.readBytes(url);
//After download is completed
//If filePath is not passed in as parameter
if (path == null) {
//Use getPathForWallpaperFile to get a path for a wallpaper file
path = await getPathForWallpaperFile(url);
}
// ...
return 0;
}
静态未来下载壁纸文件(int-wallperId,{String-path})异步{
// ...
//开始从url下载墙纸文件
最终缓冲区=等待http.readBytes(url);
//下载完成后
//如果文件路径未作为参数传入
if(路径==null){
//使用getPathForWallpaperFile获取墙纸文件的路径
路径=等待GetPathForWallperFile(url);
}
// ...
返回0;
}
顺便说一句,
//
保留用于类和字段的文档,使用//
进行方法内注释
我也不确定使用同步io操作是否是个好主意。这可能会阻塞应用程序的UI,最好使用异步io api(再次使用
wait
)。什么不起作用?