Home > Enterprise >  ('Widget? Function(BuildContext)') isn't a valid override of 'SearchDelegate.bui
('Widget? Function(BuildContext)') isn't a valid override of 'SearchDelegate.bui

Time:05-18

We have recently migrated our app to null safety and now our SearchDelegate is returning the below error

'AddressSearch.buildResults' ('Widget? Function(BuildContext)') isn't a valid override of 'SearchDelegate.buildResults' ('Widget Function(BuildContext)').

It seems to be related to the results but we cannot see how this changed since going to null safety

class AddressSearch extends SearchDelegate<Suggestion?> {
  AddressSearch(this.sessionToken) {
    apiClient = PlaceApiProvider(sessionToken);
  }

  final String sessionToken;
  late PlaceApiProvider apiClient;

  @override
  String get searchFieldLabel => 'Enter Task Suburb';

  @override
  List<Widget> buildActions(BuildContext context) {
    return [
      query.isNotEmpty
          ? IconButton(
              tooltip: 'Clear',
              icon: const Icon(Icons.clear),
              onPressed: () {
                query = '';
              },
            )
          : const SizedBox()
    ];
  }

  @override
  Widget buildLeading(BuildContext context) {
    return IconButton(
      tooltip: 'Back',
      icon: Icon(Icons.adaptive.arrow_back, color: Colors.black),
      onPressed: () {
        close(context, null);
      },
    );
  }

  @override
  Widget? buildResults(BuildContext context) {
    return Text(query);
  }

  @override
  Widget buildSuggestions(BuildContext context) {
    return FutureBuilder(
        future: query.isEmpty
            ? null
            : apiClient.fetchSuggestions(
                query, Localizations.localeOf(context).languageCode),
        builder: (context, AsyncSnapshot snapshot) => query.isEmpty
            ? Container(
                padding: const EdgeInsets.all(16.0),
                child: const Text(''),
              )
            : snapshot.hasData
                ? ListView.builder(
                    itemBuilder: (context, index) => ListTile(
                      title: Text(
                          (snapshot.data[index] as Suggestion).description!),
                      onTap: () {
                        close(context, snapshot.data![index] as Suggestion?);
                      },
                    ),
                    itemCount: snapshot.data!.length,
                  )
                : const SizedBox());
  }
}

CodePudding user response:

just remove the question mark from

Widget? buildResults(BuildContext context) {

so like

Widget buildResults(BuildContext context) {
  • Related