Home > Blockchain >  The return type 'List<String>?' isn't a 'Future<Iterable<_>>&#
The return type 'List<String>?' isn't a 'Future<Iterable<_>>&#

Time:06-25

I am trying to use flutter [Typeahead]https://pub.dev/packages/flutter_typeahead package for displaying suggestion for the user. I get "The return type 'List?' isn't a 'Future<Iterable<_>>', as required by the closure's context". I followed the example in the documentation but can't make it work.

TypeAheadField(
    textFieldConfiguration: TextFieldConfiguration(
        autofocus: true,
        decoration: InputDecoration(
            filled: true,
            fillColor: Colors.white,
            hintText: 'Search Location',
            border: OutlineInputBorder(
                borderRadius: BorderRadius.circular(30.0),
            ),
        ),
    ),
    suggestionsCallback: (searchPattern) async {
        return await PlacesRepository.fetchPlaceSuggestions(searchPattern); /// error here
    },
),

fetchPlaceSuggestions():

  Future<List<String>?> fetchPlaceSuggestions(String searchText) async {
    // Fetch result for the search pattern
    final response =
        await _dioClient.get('{$_baseUrl}autocomplete/json?', queryParameters: {
      'input': searchText,
      'key': googleAPIKey,
    });

    // Check if response is successfull
    if (response.statusCode == 200) {
      return List.generate(response.data['predictions'].length, (index) {
        return response.data['predictions'][index];
      });
    }
    return null;
  }

Thanks. Cheers

CodePudding user response:

Firstly, remove async in return await PlacesRepository.fetchPlaceSuggestions(searchPattern);, or what you get is a value, not a Future<value>, which is not compatible with the suggestionsCallback field type declaration.

Secondly, remove the question mark in Future<List<String>?>, the reason is the same. And then it works.

Demo code:

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Scroll'),
      ),
      bottomNavigationBar: BottomNavigationBar(
        // currentIndex: _selectedScreenIndex,
        // onTap: _selectScreen,
        items: const [
          BottomNavigationBarItem(icon: Icon(Icons.home), label: 'Screen A'),
          BottomNavigationBarItem(icon: Icon(Icons.settings), label: "Screen B")
        ],
      ),
      body: Container(
        child: TypeAheadField(
          textFieldConfiguration: TextFieldConfiguration(
              autofocus: true,
              style: DefaultTextStyle.of(context)
                  .style
                  .copyWith(fontStyle: FontStyle.italic),
              decoration: InputDecoration(border: OutlineInputBorder())),
          suggestionsCallback: (pattern) async {
            return fetchPlaceSuggestions();
          },
          itemBuilder: (context, suggestion) {
            return ListTile(
              leading: Icon(Icons.shopping_cart),
            );
          },
          onSuggestionSelected: (suggestion) {
            // Navigator.of(context).push(MaterialPageRoute(
            //     builder: (context) => ProductPage(product: suggestion)));
            print("to page .......");
          },
        ),
      ),
    );
  }

  Future<List<String>> fetchPlaceSuggestions() async {
    return Future.value(List.generate(2, (index) {
      return ['a', 'b'][index];
    }));
  }
  • Related