Skip to content

Commit fe3c1c9

Browse files
fix: improve code
1 parent 2502e7d commit fe3c1c9

File tree

2 files changed

+14
-12
lines changed

2 files changed

+14
-12
lines changed
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
extension GenericValidator<T> on T? {
2+
bool emptyValidator() {
3+
if (this == null) return true;
4+
if (this is Iterable) return (this as Iterable).isEmpty;
5+
if (this is String) return (this as String).isEmpty;
6+
if (this is List) return (this as List).isEmpty;
7+
if (this is Map) return (this as Map).isEmpty;
8+
if (this is Set) return (this as Set).isEmpty;
9+
return false;
10+
}
11+
}

lib/src/fields/form_builder_dropdown.dart

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import 'package:flutter/foundation.dart';
22
import 'package:flutter/material.dart';
33
import 'package:flutter_form_builder/flutter_form_builder.dart';
4+
import 'package:flutter_form_builder/src/extensions/generic_validator.dart';
45

56
/// Field for Dropdown button
67
class FormBuilderDropdown<T> extends FormBuilderFieldDecoration<T> {
@@ -309,19 +310,9 @@ class _FormBuilderDropdownState<T>
309310

310311
if ((!listEquals(oldChilds, currentlyChilds) ||
311312
!listEquals(oldValues, currentlyValues)) &&
312-
(widget.items.contains(initialValue) ||
313-
_emptyValidator<T>(initialValue))) {
313+
(currentlyValues.contains(initialValue) ||
314+
initialValue.emptyValidator())) {
314315
setValue(initialValue);
315316
}
316317
}
317318
}
318-
319-
bool _emptyValidator<T>(T? value) {
320-
if (value == null) return true;
321-
if (value is Iterable) return value.isEmpty;
322-
if (value is String) return value.isEmpty;
323-
if (value is List) return value.isEmpty;
324-
if (value is Map) return value.isEmpty;
325-
if (value is Set) return value.isEmpty;
326-
return false;
327-
}

0 commit comments

Comments
 (0)