You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Autocomplete - Allow rendering of the options view when 'optionsBuilder' returns an empty iterable #186208
I want to show a text when there are no options that match the entered text in my Autocomplete widget.
To accomplish this, my plan was to modify the optionsViewBuilder to show a Text when options are empty. However, this is not possible since optionsViewBuilder is never called when optionsBuilder returns an empty iterable.
Proposal
My first proposal would be to always call optionsViewBuilder regardless of the result from optionsBuilder.
However, I can understand that this could potentially alter significantly the Autocomplete's behavior. Therefore, a property could be added to that widget to control when optionsViewBuilder should be called (i.e. always or only when options are not empty).
This could also serve when it is necessary to show a loading indicator while the options are loading. To implement this, while loading is in progress, an empty iterable could be returned by optionsBuilder. By setting the widget to always render the optionsViewBuilder regardless of whether there are options or not, it would become possible to show a loading indicator in the overlay.
Hi @mlizee-epsilia, thank you for proposing this feature. I'm curious if your use-case is already possible with the example below.
The example returns an iterable with a placeholder string in optionsBuilder when the matches are empty, then in optionsViewBuilder it is used as a signal whether to return a normal list of options or a single Text widget.
import'dart:math';
import'package:flutter/material.dart';
/// Flutter code sample for [RawAutocomplete].voidmain() =>runApp(constAutocompleteExampleApp());
classAutocompleteExampleAppextendsStatelessWidget {
constAutocompleteExampleApp({super.key});
@overrideWidgetbuild(BuildContext context) {
returnMaterialApp(
home:Scaffold(
appBar:AppBar(title:constText('RawAutocomplete Form')),
body:constCenter(child:AutocompleteFormExample()),
),
);
}
}
classAutocompleteFormExampleextendsStatefulWidget {
constAutocompleteFormExample({super.key});
@overrideAutocompleteFormExampleStatecreateState() =>AutocompleteFormExampleState();
}
classAutocompleteFormExampleStateextendsState<AutocompleteFormExample> {
String? _autocompleteSelection;
// The dynamic list of options that will be populated by the buttonfinalList<String> _currentOptions =<String>[];
finalRandom _random =Random();
// A unique string to identify when there are no options availablestaticconstString _emptyPlaceholder ='NO_OPTIONS_PLACEHOLDER';
void_addRandomOptions() {
setState(() {
for (int i =0; i <5; i++) {
_currentOptions.add('Option ${_random.nextInt(1000)}');
}
});
}
@overrideWidgetbuild(BuildContext context) {
returnPadding(
padding:constEdgeInsets.all(8.0),
child:Column(
crossAxisAlignment:CrossAxisAlignment.start,
children:<Widget>[
constText(
'Description: This example shows a RawAutocomplete form field.',
),
constSizedBox(height:16.0),
ElevatedButton(
onPressed: _addRandomOptions,
child:constText('Add 5 Random Options'),
),
constSizedBox(height:16.0),
RawAutocomplete<String>(
optionsBuilder: (TextEditingValue textEditingValue) {
// Filter the current options based on the inputfinalIterable<String> matches = _currentOptions.where(
(String option) => option.toLowerCase().contains(
textEditingValue.text.toLowerCase(),
),
);
// If no matches (or list is completely empty), return the placeholderif (matches.isEmpty) {
returnconst [_emptyPlaceholder];
}
return matches;
},
onSelected: (String selection) {
setState(() {
_autocompleteSelection = selection;
});
},
fieldViewBuilder: (
BuildContext context,
TextEditingController textEditingController,
FocusNode focusNode,
VoidCallback onFieldSubmitted,
) {
returnTextFormField(
controller: textEditingController,
decoration:constInputDecoration(
hintText:'Type to search options...',
),
focusNode: focusNode,
onFieldSubmitted: (String value) {
onFieldSubmitted();
},
validator: (String? value) {
if (value ==null||!_currentOptions.contains(value)) {
return'Nothing valid selected.';
}
returnnull;
},
);
},
optionsViewBuilder: (
BuildContext context,
AutocompleteOnSelected<String> onSelected,
Iterable<String> options,
) {
// Check if the only option is our empty placeholderif (options.length ==1&& options.first == _emptyPlaceholder) {
returnAlign(
alignment:Alignment.topLeft,
child:Material(
elevation:4.0,
child:Container(
padding:constEdgeInsets.all(16.0),
width:300.0,
child:constText(
'No options available',
style:TextStyle(color:Colors.grey),
),
),
),
);
}
// Otherwise, render the standard list of optionsreturnAlign(
alignment:Alignment.topLeft,
child:Material(
elevation:4.0,
child:SizedBox(
height:300.0,
width:300.0, // Added width to constrain the popup
child:ListView.builder(
padding:constEdgeInsets.all(8.0),
itemCount: options.length,
itemBuilder: (BuildContext context, int index) {
finalString option = options.elementAt(index);
returnListTile(
title:Text(option),
onTap: () {
onSelected(option);
},
);
},
),
),
),
);
},
),
],
),
);
}
}
@Renzo-Olivares Thanks for your detailed example! Indeed, it achieves the wanted result.
However, I feel that the implementation is really complicated for a simple feature. I still think that a more simple solution should exist.
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of flutter doctor -v and a minimal reproduction of the issue.
Activity
saurabh-mirajkar commentedon May 8, 2026
@mlizee-epsilia Thanks for the detailed proposal. Labeling this to
team-text-inputfor further consideration.Renzo-Olivares commentedon May 8, 2026
Hi @mlizee-epsilia, thank you for proposing this feature. I'm curious if your use-case is already possible with the example below.
The example returns an iterable with a placeholder string in
optionsBuilderwhen the matches are empty, then inoptionsViewBuilderit is used as a signal whether to return a normal list of options or a single Text widget.mlizee-epsilia commentedon May 8, 2026
@Renzo-Olivares Thanks for your detailed example! Indeed, it achieves the wanted result.
However, I feel that the implementation is really complicated for a simple feature. I still think that a more simple solution should exist.
Renzo-Olivares commentedon May 8, 2026
Sounds good, we can keep this proposal open. Workaround for now can be found in #186208 (comment)
victorsanni commentedon May 8, 2026
Thanks for filing this issue and the quick responses, however this is a duplicate of an existing issue: Autocomplete widget calls optionsViewBuilder even when optionsBuilder return empty Iterable. Deduping to that issue since it has been open for longer, has more subscribers etc. But thanks for filing!
github-actions commentedon May 22, 2026
This thread has been automatically locked since there has not been any recent activity after it was closed. If you are still experiencing a similar issue, please open a new bug, including the output of
flutter doctor -vand a minimal reproduction of the issue.