how to do suggestionCallback in typeahead flutter?


i’m beginner in flutter, in my flutter project i used flutter_typeahead package but i did not able to execute this code.

i did not get proper guidance from this documentation

    suggestionsCallback: (pattern) {
      return CitiesService.getSuggestions(pattern);


The example service is here, pattern in CitiesService example means contain some characters

import 'dart:math';

class BackendService {
  static Future<List> getSuggestions(String query) async {
    await Future.delayed(Duration(seconds: 1));

    return List.generate(3, (index) {
      return {'name': query + index.toString(), 'price': Random().nextInt(100)};

class CitiesService {
  static final List<String> cities = [
    'San Fransisco',
    'Los Angeles',
    'New York City',

  static List<String> getSuggestions(String query) {
    List<String> matches = List();

    matches.retainWhere((s) => s.toLowerCase().contains(query.toLowerCase()));
    return matches;

Answered By – chunhunghan

Answer Checked By – Mary Flores (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published.