Skip to content

Commit 471f31c

Browse files
committed
Allow null initialValue for CountryPicker. Closes #421
1 parent fb9bf99 commit 471f31c

File tree

2 files changed

+12
-7
lines changed

2 files changed

+12
-7
lines changed

lib/src/country_picker_util.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import 'package:country_pickers/country.dart';
44
class CountryPickerUtil {
55
static Country _getCountryByField(
66
String Function(Country) fieldAccessor, String query) {
7-
final queryUpperCase = query.toUpperCase();
7+
final queryUpperCase = query?.toUpperCase();
88
return countryList.firstWhere(
99
(country) => fieldAccessor(country).toUpperCase() == queryUpperCase,
1010
orElse: () => null);

lib/src/fields/form_builder_country_picker.dart

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,13 @@ class FormBuilderCountryPicker extends StatefulWidget {
3030
final bool isCupertinoPicker;
3131
final double cupertinoPickerSheetHeight;
3232
final Color cursorColor;
33+
final String placeholderText;
3334

3435
FormBuilderCountryPicker({
3536
Key key,
3637
@required this.attribute,
37-
this.defaultSelectedCountryIsoCode = 'US',
38-
@required this.initialValue,
38+
this.defaultSelectedCountryIsoCode,
39+
this.initialValue,
3940
this.validators = const [],
4041
this.readOnly = false,
4142
this.decoration = const InputDecoration(),
@@ -53,8 +54,8 @@ class FormBuilderCountryPicker extends StatefulWidget {
5354
this.isCupertinoPicker = false,
5455
this.cupertinoPickerSheetHeight,
5556
this.cursorColor,
56-
}) : assert(initialValue != null),
57-
super(key: key);
57+
this.placeholderText = 'Select country',
58+
}) : super(key: key);
5859

5960
@override
6061
_FormBuilderCountryPickerState createState() =>
@@ -117,10 +118,14 @@ class _FormBuilderCountryPickerState extends State<FormBuilderCountryPicker> {
117118
child: Row(
118119
key: ObjectKey(field.value),
119120
children: [
120-
CountryPickerUtils.getDefaultFlagImage(field.value),
121+
if (field.value != null)
122+
CountryPickerUtils.getDefaultFlagImage(field.value),
121123
SizedBox(width: 10),
122124
Expanded(
123-
child: Text(field.value?.name ?? '', style: widget.style),
125+
child: Text(
126+
field.value?.name ?? widget.placeholderText ?? '',
127+
style: widget.style,
128+
),
124129
),
125130
],
126131
),

0 commit comments

Comments
 (0)