-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
nullable_object_extensions.dart
38 lines (36 loc) · 1.31 KB
/
nullable_object_extensions.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
31
32
33
34
35
36
37
38
part of '/katana.dart';
/// Provides an extension method for [List] that is nullable.
///
/// Nullableな[Object]用の拡張メソッドを提供します。
extension NullableObjectExtensions on Object? {
/// Returns [defaultValue] if an object of type [T] is [Null] or not of type [T].
///
/// It may be more concise than writing default values with the `??` operator may be more concise than the default value.
///
/// [T]型のオブジェクトが[Null]の場合、もしくは[T]型でない場合[defaultValue]を返します。
///
/// `??`演算子でデフォルト値を記述するより簡潔に書ける場合があります。
T def<T>(T defaultValue) {
if (this == null || this is! T) {
return defaultValue;
}
return this as T;
}
/// If this object is Json encodable, `true` is returned.
///
/// If a [List] or [Map] exists, its contents are also checked.
///
/// Cannot be applied to `dynamic` types.
///
/// このオブジェクトがJsonでエンコード可能な場合`true`が返されます。
///
/// [List]や[Map]が存在していた場合はその中身までチェックされます。
///
/// `dynamic`型には適用できません。
bool get isJsonEncodable {
if (this == null) {
return false;
}
return jsonEncodable(this);
}
}