【发布时间】:2018-08-17 02:33:15
【问题描述】:
我想在 function1 完成后调用 function2。 为此,我确实这样做了。
这是功能1。
Future _uploadImages() async {
setState(() {isUploading = true;});
images.forEach((image) async {
await image.requestThumbnail(300, 300).then((_) async {
final int date = DateTime.now().millisecondsSinceEpoch;
final String storageId = '$date$uid';
final StorageReference ref =
FirebaseStorage.instance.ref().child('images').child(storageId);
final file = image.thumbData.buffer.asUint8List();
StorageUploadTask uploadTask = ref.putData(file);
Uri downloadUrl = (await uploadTask.future).downloadUrl;
final String url = downloadUrl.toString();
imageUrls.add(url);
});
});
}
这是功能2
Future _writeImageInfo() async {
await _uploadImages().then((_) async {
await Firestore.instance.collection('post').document(uid).setData({
'imageUrls': imageUrls,
}).then((_) {
Navigator.of(context).pop();
});
}
但是控制台说函数 2 的 imageUrls 在列表长度 = 0 时被调用,因为它是在函数 1 完成之前调用的。 我不知道为什么在函数 1 之后没有调用该函数。 我怎样才能做到这一点?
【问题讨论】:
-
检查这可能对你有帮助dartlang.org/tutorials/language/…
-
感谢您的评论,我按照文档所说的做了,但是当列表为空时仍然调用。
标签: flutter