how to do suggestionCallback in typeahead flutter?

Issue

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 https://pub.dev/documentation/flutter_typeahead/latest/flutter_typeahead/flutter_typeahead-library.html

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

Solution

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

https://github.com/AbdulRahmanAlHamali/flutter_typeahead/blob/master/example/lib/data.dart

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 = [
    'Beirut',
    'Damascus',
    'San Fransisco',
    'Rome',
    'Los Angeles',
    'Madrid',
    'Bali',
    'Barcelona',
    'Paris',
    'Bucharest',
    'New York City',
    'Philadelphia',
    'Sydney',
  ];

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

    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.