@@ -28,7 +28,7 @@ class AttachmentFile {
28
28
'File by path is not supported in web, Please provide bytes' ,
29
29
),
30
30
assert (
31
- name? . contains ('.' ) ?? true ,
31
+ name == null || name.isEmpty || name. contains ('.' ),
32
32
'Invalid file name, should also contain file extension' ,
33
33
),
34
34
_name = name;
@@ -47,8 +47,10 @@ class AttachmentFile {
47
47
final String ? _name;
48
48
49
49
/// File name including its extension.
50
- String ? get name =>
51
- _name ?? path? .split (CurrentPlatform .isWindows ? r'\' : '/' ).last;
50
+ String ? get name {
51
+ if (_name case final name? when name.isNotEmpty) return name;
52
+ return path? .split (CurrentPlatform .isWindows ? r'\' : '/' ).last;
53
+ }
52
54
53
55
/// Byte data for this file. Particularly useful if you want to manipulate
54
56
/// its data or easily upload to somewhere else.
@@ -69,22 +71,18 @@ class AttachmentFile {
69
71
70
72
/// Converts this into a [MultipartFile]
71
73
Future <MultipartFile > toMultipartFile () async {
72
- MultipartFile multiPartFile;
73
-
74
- if (CurrentPlatform .isWeb) {
75
- multiPartFile = MultipartFile .fromBytes (
76
- bytes! ,
77
- filename: name,
78
- contentType: mediaType,
79
- );
80
- } else {
81
- multiPartFile = await MultipartFile .fromFile (
82
- path! ,
83
- filename: name,
84
- contentType: mediaType,
85
- );
86
- }
87
- return multiPartFile;
74
+ return switch (CurrentPlatform .type) {
75
+ PlatformType .web => MultipartFile .fromBytes (
76
+ bytes! ,
77
+ filename: name,
78
+ contentType: mediaType,
79
+ ),
80
+ _ => await MultipartFile .fromFile (
81
+ path! ,
82
+ filename: name,
83
+ contentType: mediaType,
84
+ ),
85
+ };
88
86
}
89
87
90
88
/// Creates a copy of this [AttachmentFile] but with the given fields
@@ -106,7 +104,7 @@ class AttachmentFile {
106
104
107
105
/// Union class to hold various [UploadState] of a attachment.
108
106
@freezed
109
- class UploadState with _$UploadState {
107
+ sealed class UploadState with _$UploadState {
110
108
// Dummy private constructor in order to use getters
111
109
const UploadState ._();
112
110
0 commit comments