2

I'm trying to fetch data using Algolia Search in Flutter and the data does get fetched the problem is that it gives the following error:

DioException (DioException [connection timeout]: The request connection took longer than 0:00:10.000000 and it was aborted. To get rid of this exception, try raising the RequestOptions.connectTimeout above the duration of 0:00:10.000000 or improve the response time of the server.

This shouldn't even be happening considering the data is fetched in less than 1 second. Below is the fetch logic and Algolia:

import 'package:algoliasearch/algoliasearch.dart';

final client = SearchClient(
  appId: '',
  apiKey: '',
  options: ClientOptions(
    readTimeout: const Duration(seconds: 100),
    connectTimeout: const Duration(seconds: 100),
  ),
);

final userIndex = 'users';
final orderIndex = 'orders';

Future<void> fetchStaff({int page = 0}) async {
    isFetching();

    if (restaurantID == null) {
      await loadRestaurantInfo();
    }

    int hitsPerPage = selectedItem == 'Show Entries'
        ? 5
        : int.tryParse(selectedItem) ?? 5;

    List<StaffModel> matchedStaff = [];

    final query = SearchForHits(
      indexName: userIndex,
      query: searchController.text.trim().isEmpty
          ? null
          : searchController.text.trim(),
      filters:
          'restaurantID:$restaurantID AND (type:kitchen-admin OR type:kitchen-cook)',
      hitsPerPage: hitsPerPage,
      page: page,
    );

    final response = await client.searchIndex(request: query);
    totalPages = response.nbPages ?? 1;
    currentPage = page + 1;

    matchedStaff.addAll(
      response.hits.map((hit) {
        final data = Map<String, dynamic>.from(hit);
        return StaffModel.fromMap(data, data['id']);
      }),
    );

    allStaffList = matchedStaff;
    updatePaginatedList();
    isFetching();
  }

I've upped the limit in Algolia search unto 5 minutes but that's clearly not where the error is stemming from since it just says timeout in 5 minutes, even though it doesn't take that long for the exception to show.

Edit: One other weird thing I forgot to mention about it is that wrapping it in a try-catch block doesn't catch it. It still just silently throws the exception in the background.

Minimal Reproducible Code:

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key,});

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  @override
  void initState() {
    super.initState();
    retrieveSchedules();
  }

  Future<void> retrieveSchedules() async {
    try {
      final query = SearchForHits(
        indexName: userIndex,
        hitsPerPage: 1,
      );

      final response = await client.searchIndex(request: query);
      print(response.hits.first);
    } catch (e) {
      print(e);
    } finally {}
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(body: Text('Minimal Reproducible Code'));
  }
}

final client = SearchClient(
  appId: '',
  apiKey: '',
  options: ClientOptions(
    readTimeout: const Duration(seconds: 100),
    connectTimeout: const Duration(seconds: 100),
  ),
);

final userIndex = 'users';
12
  • can you confirm the connectionTImeout by printing it like this? print(client.options.connectTimeout); Commented Jun 30 at 12:08
  • Yeah, sure. final response = await client.searchIndex(request: query); log("Timeout Confirmation ${client.options.connectTimeout}"); Timeout Confirmation 0:01:40.000000 Commented Jul 1 at 4:29
  • @MunsifAli I've shared the output above Commented Jul 2 at 4:31
  • as the connection timeout is not same as in the given Exception. it might not be the agolia issue. as I myself using it and I didn't notice any issue in your code. can you debug other parts of the code which might be causing this issue. Commented Jul 2 at 17:52
  • @MunsifAli I've tried isolating the code. Created a new project and only made a fetch request no extra padding code but the exception is still occurring. So clearly a problem related to this package or code. This is the output of the new one and its the same in both: The request connection took longer than 0:01:40.000000 and it was aborted. To get rid of this exception, try raising the RequestOptions.connectTimeout above the duration of 0:01:40.000000 (SocketException: HTTP connection timed out after 0:01:40.000000, host: .algolia.net, port: 443) Print Output: 0:01:40.000000 Commented Jul 3 at 5:44

1 Answer 1

0

Try downgrade your flutter version and also algolia version which suitable for both, don't jump into always the latest flutter cause some packages are need time to catch up with latest release.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.