async await??

This commit is contained in:
Inex Code 2022-04-21 06:49:59 +00:00
parent 473828f498
commit b6b58e8895

View file

@ -75,7 +75,7 @@ Future<void> _postOnMastodon(reqJson) async {
final postText = reqJson['object']['text'] ?? ''; final postText = reqJson['object']['text'] ?? '';
final attachments = reqJson['object']['attachments'] ?? []; final attachments = reqJson['object']['attachments'] ?? [];
final photos = final photos =
attachments.where((attachment) => attachment['type'] == 'photo'); attachments.where((attachment) => attachment['type'] == 'photo');
@ -103,12 +103,14 @@ Future<void> _postOnMastodon(reqJson) async {
if (photos.isEmpty) { if (photos.isEmpty) {
await dio.post('$instanceUrl/api/v1/statuses', await dio.post('$instanceUrl/api/v1/statuses',
data: {'status': postText}, data: {'status': postText},
options: dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})); options:
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
} else { } else {
final mediaResponses = await Future.wait( final mediaResponses = await photoUrls.map((photoUrl) async => await dio.post(
photoUrls.map((photoUrl) async => dio.post('$instanceUrl/api/v1/media', '$instanceUrl/api/v1/media',
data: dio_lib.FormData.fromMap({'file': File(photoUrl)}), data: dio_lib.FormData.fromMap({'file': File(photoUrl)}),
options: dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})))); options:
dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'})));
final mediaIds = mediaResponses.map((mediaResponse) { final mediaIds = mediaResponses.map((mediaResponse) {
final mediaId = mediaResponse.data['id']; final mediaId = mediaResponse.data['id'];
@ -116,11 +118,9 @@ Future<void> _postOnMastodon(reqJson) async {
}).toList(); }).toList();
await dio.post('$instanceUrl/api/v1/statuses', await dio.post('$instanceUrl/api/v1/statuses',
data: { data: {'status': postText, 'media_ids': mediaIds},
'status': postText, options:
'media_ids': mediaIds dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
},
options: dio_lib.Options(headers: {'Authorization': 'Bearer $authToken'}));
await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete())); await Future.wait(photoUrls.map((photoUrl) => File(photoUrl).delete()));
} }