Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Minimum chars before sending request? #83

Closed
wiradikusuma opened this issue Apr 6, 2019 · 9 comments
Closed

Minimum chars before sending request? #83

wiradikusuma opened this issue Apr 6, 2019 · 9 comments

Comments

@wiradikusuma
Copy link

How do I set minimum chars before suggestionsCallback is triggered?

@sjmcdowall
Copy link
Collaborator

sjmcdowall commented Apr 6, 2019 via email

@wiradikusuma
Copy link
Author

That's actually a good idea. Thanks!

@claudelk
Copy link

claudelk commented May 2, 2019

@sjmcdowall can we reopen this one? Checking the pattern length fires an exception, even if the pattern is not empty (and not null):

Code:

TypeAheadField(
            textFieldConfiguration: TextFieldConfiguration(
                focusNode: _recipientsFocusNode,
                autofocus: true,
                /*style: DefaultTextStyle.of(context)
                .style
                 .copyWith(fontStyle: FontStyle.italic) */
                decoration: InputDecoration(labelText: sField)
                /* decoration: InputDecoration(border: OutlineInputBorder()) */
                ),
            suggestionsCallback: (value) async {
              if (value != null && value != '') {
                if (value.startsWith('@') && value.length >= 2)
                  await model.getNamesBySuggestion(value);
              } else
                return null;
            },
            itemBuilder: (context, suggestion) {
              return SingleChildScrollView(
                child: ListTile(
                  leading: Icon(Icons.portrait), // to change
                  title: Text(suggestion), //to change
                  //subtitle: Text('\$${suggestion['price']}'), // to change
                ),
              );
            },
            onSuggestionSelected: (suggestion) {
              //set the full name as part of the field, add a coma and allow the user to select another recipient
              //Navigator.of(context).push(MaterialPageRoute(builder: (context) => {}));
            },
          )

Result:

[VERBOSE-2:ui_dart_state.cc(148)] Unhandled Exception: NoSuchMethodError: The getter 'length' was called on null.
Receiver: null
Tried calling: length
#0      Object.noSuchMethod (dart:core/runtime/libobject_patch.dart:50:5)
#1      _SuggestionsListState._getSuggestions.<anonymous closure> (package:flutter_typeahead/flutter_typeahead.dart:1032:44)
#2      State.setState (package:flutter/src/widgets/framework.dart:1122:30)
#3      _SuggestionsListState._getSuggestions (package:flutter_typeahead/flutter_typeahead.dart:1030:9)
<asynchronous suspension>
#4      _SuggestionsListState.initState.<anonymous closure>.<anonymous closure> (package:flutter_typeahead/flutter_typeahead.dart:993:20)
<asynchronous suspension>
#5      _rootRun (dart:async/zone.dart:1120:38)
#6      _CustomZone.run (dart:async/zone.dart:1021:19)
#7      _CustomZone.runGuarded (dart:async/zone.dart:923:7)
#8      _CustomZone.bindCallbackGuarded.<anonymous closure> (dart:async/zone.dart:963:23)
#9      _rootRun (dart:async/zone.dart:112<…>

@claudelk
Copy link

claudelk commented May 2, 2019

Fixed it with this:

suggestionsCallback: (value) async {
              if (value != null && value != '') {
                if (value.startsWith('@') && value.length >= 3)
                  return await model.getNamesBySuggestion(value);
                else
                  return [];
              } else
                return [];
            },

Always making sure to return an empty list if no call.

@KaYBlitZ
Copy link
Contributor

That's pretty interesting. I am going to make a fix that will allow returning null. If you return an empty list, then it will show the no items! box. If you return null, a suggestions box will not show at all.

@qbait
Copy link

qbait commented Feb 8, 2021

@KaYBlitZ have you implemented it at the end?

@MOhsain
Copy link

MOhsain commented May 1, 2021

@qbait @KaYBlitZ @claudelk @alphamikle I was using this version flutter_typeahead: ^1.7.0. This version was working fine but now i ugrade this to 3.1.1. now it is not showing any list.
if i pass empty list then it show not item found error. kindly help me
And here is my Code ->
suggestionsCallback: (value) async {
if (value != null && value != '') {
if (value.length >= 1)
return await UserData.getSuggestions(value);
else
return null;
} else
return null;
},

@sjmcdowall
Copy link
Collaborator

I think it's the null protection added in 3.x. You shouldn't really return a null but instead an empty set of whatever type UserData.getSuggestions(value) would return .. just make it an empty array or whatever of that type?

@MOhsain
Copy link

MOhsain commented May 1, 2021

UserData.getSuggestions its a just String Array.
When i pass empty list " return [[ " instead of " return null ", its showing no item found error....
So kindly guide me. Have you tried this like suggestions will be shown after 2 charectors with 3.x?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants