forked from vandadnp/flutter-tips-and-tricks
-
Notifications
You must be signed in to change notification settings - Fork 1
/
future-unwrap-in-flutter.dart
30 lines (27 loc) · 1.05 KB
/
future-unwrap-in-flutter.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
// 🐦 Twitter https://twitter.com/vandadnp
// 🔵 LinkedIn https://linkedin.com/in/vandadnp
// 🎥 YouTube https://youtube.com/c/vandadnp
// 💙 Free Flutter Course https://linktr.ee/vandadnp
// 📦 11+ Hours Bloc Course https://youtu.be/Mn254cnduOY
// 🔶 7+ Hours MobX Course https://youtu.be/7Od55PBxYkI
// 🦄 8+ Hours RxSwift Coursde https://youtu.be/xBFWMYmm9ro
// 🤝 Want to support my work? https://buymeacoffee.com/vandad
import 'dart:async';
import 'dart:io';
import 'package:image_picker/image_picker.dart';
extension Unwrap<T> on Future<T?> {
Future<T> unwrap() => then(
(value) => value != null
? Future<T>.value(value)
: Future.any([]),
);
}
@immutable
class ImagePickerHelper {
static final ImagePicker _imagePicker = ImagePicker();
static Future<File> pickImageFromGallery() => _imagePicker
.pickImage(source: ImageSource.gallery)
.unwrap()
.then((xFile) => xFile.path)
.then((filePath) => File(filePath));
}