Skip to content

Commit

Permalink
feat: allow returning null in suggestions callback
Browse files Browse the repository at this point in the history
  • Loading branch information
clragon committed Jan 11, 2024
1 parent 68b8df4 commit f53061e
Show file tree
Hide file tree
Showing 3 changed files with 5 additions and 3 deletions.
2 changes: 1 addition & 1 deletion lib/src/common/base/types.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import 'dart:async';
import 'package:flutter/widgets.dart';

/// Called to retrieve the suggestions for [search].
typedef SuggestionsCallback<T> = FutureOr<List<T>> Function(String search);
typedef SuggestionsCallback<T> = FutureOr<List<T>?> Function(String search);

/// Builds a widget for a suggestion in the suggestions box.
typedef ItemBuilder<T> = Widget Function(BuildContext context, T value);
Expand Down
4 changes: 3 additions & 1 deletion lib/src/common/box/suggestions_list.dart
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,7 @@ class _SuggestionsListState<T> extends State<SuggestionsList<T>> {
bool retainOnLoading = widget.retainOnLoading ?? true;

bool isError = widget.controller.hasError;
bool isEmpty = suggestions == null || suggestions.isEmpty;
bool isEmpty = suggestions?.isEmpty ?? false;
bool isLoading =
widget.controller.isLoading && (isEmpty || !retainOnLoading);

Expand All @@ -183,6 +183,8 @@ class _SuggestionsListState<T> extends State<SuggestionsList<T>> {
} else if (isEmpty) {
if (widget.hideOnEmpty ?? false) return const SizedBox();
return widget.emptyBuilder(context);
} else if (suggestions == null) {
return const SizedBox();
}

if (widget.listBuilder != null) {
Expand Down
2 changes: 1 addition & 1 deletion lib/src/common/search/suggestions_search.dart
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ class _SuggestionsSearchState<T> extends State<SuggestionsSearch<T>> {
Object? newError;

try {
newSuggestions = (await widget.suggestionsCallback(search)).toList();
newSuggestions = (await widget.suggestionsCallback(search))?.toList();
} on Exception catch (e) {
newError = e;
}
Expand Down

0 comments on commit f53061e

Please sign in to comment.