Skip to content

Commit e54a2e5

Browse files
committed
s/safeFirst/firstOrNull
1 parent 23a4c23 commit e54a2e5

File tree

6 files changed

+10
-14
lines changed

6 files changed

+10
-14
lines changed

lib/builders/utils.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ extension ClassElementX on ClassElement {
1212
ConstructorElement? get freezedConstructor => constructors
1313
.where((c) => c.isFactory && c.displayName == name)
1414
// ignore: invalid_use_of_visible_for_testing_member
15-
.safeFirst;
15+
.firstOrNull;
1616

1717
// unique collection of constructor arguments and fields
1818
Iterable<VariableElement> get relationshipFields {

lib/src/adapter/adapter.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
152152
/// Finds model of type [T] by [key] in local storage.
153153
T? findOneLocal(String? key) {
154154
if (key == null) return null;
155-
return findManyLocal([key]).safeFirst;
155+
return findManyLocal([key]).firstOrNull;
156156
}
157157

158158
T? findOneLocalById(Object id) {

lib/src/model/relationship/belongs_to.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ class BelongsTo<E extends DataModelMixin<E>> extends Relationship<E, E?> {
3636
}
3737

3838
/// Obtains the single [E] value of this relationship (`null` if not present).
39-
E? get value => _iterable.safeFirst;
39+
E? get value => _iterable.firstOrNull;
4040

4141
/// Sets the single [E] value of this relationship, replacing any previous [value].
4242
///
@@ -60,7 +60,7 @@ class BelongsTo<E extends DataModelMixin<E>> extends Relationship<E, E?> {
6060
}
6161

6262
/// Returns the [value]'s `key`.
63-
String? get key => super._keys.safeFirst;
63+
String? get key => super._keys.firstOrNull;
6464

6565
Object? get id => key != null ? _adapter.core.getIdForKey(key!) : null;
6666

lib/src/model/relationship/has_many.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class HasMany<E extends DataModelMixin<E>> extends Relationship<E, Set<E>> {
7474

7575
E get first => _iterable.first;
7676

77-
E? get safeFirst => isNotEmpty ? first : null;
77+
E? get firstOrNull => _iterable.firstOrNull;
7878

7979
bool get isEmpty => length == 0;
8080

lib/src/utils/extensions.dart

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@ part of flutter_data;
33
extension IterableX<T> on Iterable<T> {
44
@protected
55
@visibleForTesting
6-
T? get safeFirst => isNotEmpty ? first : null;
7-
8-
@protected
9-
@visibleForTesting
10-
bool containsFirst(T model) => safeFirst == model;
6+
bool containsFirst(T model) => firstOrNull == model;
117

128
@protected
139
@visibleForTesting
@@ -55,9 +51,9 @@ extension StringUtilsX on String {
5551
return '$prefix:$this';
5652
}
5753

58-
String? get namespace => split(':').safeFirst;
54+
String? get namespace => split(':').firstOrNull;
5955

60-
String? get type => split('#').safeFirst;
56+
String? get type => split('#').firstOrNull;
6157

6258
String denamespace() {
6359
// need to re-join with : in case there were other :s in the text

test/utils/misc_utils_test.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,8 @@ void main() async {
3838
});
3939

4040
test('iterable utils', () {
41-
expect([1, 2, 3].safeFirst, 1);
42-
expect([].safeFirst, isNull);
41+
expect([1, 2, 3].firstOrNull, 1);
42+
expect([].firstOrNull, isNull);
4343

4444
expect([1, 2, 3].containsFirst(1), isTrue);
4545
expect([1, 2, 3].containsFirst(2), isFalse);

0 commit comments

Comments
 (0)