Skip to content

Commit

Permalink
画像が選択されなかった時に例外ではなくnullを返すように変更
Browse files Browse the repository at this point in the history
  • Loading branch information
k-shunnsuke committed Jul 24, 2023
1 parent 25fdbd8 commit 9e0ccb5
Showing 1 changed file with 19 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,37 @@
// ignore_for_file: avoid_catches_without_on_clauses

import 'dart:io';

import 'package:image_picker/image_picker.dart';


enum PickType {
camera,
gallery,
}

final ImagePicker _picker = ImagePicker();

///一枚のみ選択する場合
///写真を撮影するか選択するかはenumのPickTypeで渡す
Future<File> fetchSingleImage(PickType pickType) async {
final _imageFile = await _picker.pickImage(
source:
pickType == PickType.camera ? ImageSource.camera : ImageSource.gallery,
);
if (_imageFile == null) {
throw Exception('画像が選択されませんでした。');
Future<File?> fetchSingleImage(PickType pickType) async {
try {
final _imageFile = await _picker.pickImage(
source: pickType == PickType.camera
? ImageSource.camera
: ImageSource.gallery,
);
return _imageFile == null ? null : File(_imageFile.path);
} catch (e) {
return null;
}
return File(_imageFile.path);
}

///複数選択する場合
Future<List<File>> fetchMultipleImage() async {
final _imageFiles = await _picker.pickMultiImage();
return _imageFiles.map((e) => File(e.path)).toList();
try {
final _imageFiles = await _picker.pickMultiImage();
return _imageFiles.map((e) => File(e.path)).toList();
} catch (e) {
return [];
}
}

0 comments on commit 9e0ccb5

Please sign in to comment.