From f53061ed5c8642d8bb33ac822833a3653c1b1f8d Mon Sep 17 00:00:00 2001 From: clragon Date: Thu, 11 Jan 2024 21:52:02 +0100 Subject: [PATCH] feat: allow returning null in suggestions callback --- lib/src/common/base/types.dart | 2 +- lib/src/common/box/suggestions_list.dart | 4 +++- lib/src/common/search/suggestions_search.dart | 2 +- 3 files changed, 5 insertions(+), 3 deletions(-) diff --git a/lib/src/common/base/types.dart b/lib/src/common/base/types.dart index 9e118af9..210f8e46 100644 --- a/lib/src/common/base/types.dart +++ b/lib/src/common/base/types.dart @@ -3,7 +3,7 @@ import 'dart:async'; import 'package:flutter/widgets.dart'; /// Called to retrieve the suggestions for [search]. -typedef SuggestionsCallback = FutureOr> Function(String search); +typedef SuggestionsCallback = FutureOr?> Function(String search); /// Builds a widget for a suggestion in the suggestions box. typedef ItemBuilder = Widget Function(BuildContext context, T value); diff --git a/lib/src/common/box/suggestions_list.dart b/lib/src/common/box/suggestions_list.dart index dded7f84..90db08e1 100644 --- a/lib/src/common/box/suggestions_list.dart +++ b/lib/src/common/box/suggestions_list.dart @@ -170,7 +170,7 @@ class _SuggestionsListState extends State> { 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); @@ -183,6 +183,8 @@ class _SuggestionsListState extends State> { } 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) { diff --git a/lib/src/common/search/suggestions_search.dart b/lib/src/common/search/suggestions_search.dart index 7b4f8286..3dd42c57 100644 --- a/lib/src/common/search/suggestions_search.dart +++ b/lib/src/common/search/suggestions_search.dart @@ -110,7 +110,7 @@ class _SuggestionsSearchState extends State> { Object? newError; try { - newSuggestions = (await widget.suggestionsCallback(search)).toList(); + newSuggestions = (await widget.suggestionsCallback(search))?.toList(); } on Exception catch (e) { newError = e; }