openSelect static method Null safety

Future<String?> openSelect(
  1. {required List<NativeSelectItem> items,
  2. String? defaultValue,
  3. String doneText = 'Done',
  4. String? clearText,
  5. String? title}
)

This method opens a select box that matches the native style.

items is a list of select items. The list must not be empty and the values inside of the list, must be unique. Futhermore, some of the given items must not be disabled. defaultValue iOS only, the item which is preselected. If this value is omitted, the first not disabled item is chosen automatically. doneText iOS only, the text of the done button in the toolbar. clearText if this text is given, a button which clears the selection is displayed to the user. On iOS, this button is in the toolbar next to the done button. On Android, this button is below the list of items. title Android only, if the text is given, a title is shows above the items in the dialog.

This method returns a future, which resolves the dialog is dismissed or closed. If the user, selected a item the value of the item is returned. In case the user pressed the back button on Android, or pressed the clear button, the future resolves with null.

Implementation

static Future<String?> openSelect({
  required List<NativeSelectItem> items,
  String? defaultValue,
  String doneText = 'Done',
  String? clearText,
  String? title,
}) {
  assert(items.isNotEmpty, 'Items must not be empty!');
  assert(items.any((element) => !element.disabled),
      'Not all items must be disabled!');
  assert(items.map((e) => e.value).toSet().length == items.length,
      'Values in select items must be unique!');
  assert(
      defaultValue == null ||
          items.any((element) => element.value == defaultValue),
      'Default value must be one of the given items!');

  return _channel.invokeMethod(
      'openSelect',
      jsonEncode({
        'items': [
          for (final item in items)
            {
              'value': item.value,
              'label': item.label,
              'disabled': item.disabled,
              'color': item.color?.value,
            }
        ],
        'defaultValue': defaultValue ??
            items.firstWhere((element) => !element.disabled).value,
        'doneText': doneText,
        'clearText': clearText,
        'title': title,
      }));
}