You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中使用AlgoliaSearch出现DioException[connection timeout]异常的解决求助

Flutter中使用AlgoliaSearch出现DioException[connection timeout]异常的解决求助

各位大佬好,我现在在Flutter项目里用Algolia Search拉取数据,明明数据在1秒内就返回了,但却一直抛出这个超时异常:

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.

我已经把Algolia客户端的connectTimeout和readTimeout都调到100秒了,甚至在Algolia后台把超时限制改成了5分钟,但还是没用——异常还是很快就弹出来了,根本等不到5分钟。

更奇怪的是,我用try-catch包裹请求逻辑,居然抓不到这个异常,它只会在后台默默抛出,完全没法处理。

下面是我的相关代码:

Algolia客户端初始化

import 'package:algoliasearch/algoliasearch.dart';

final client = SearchClient(
  appId: '', // 已填写实际appId
  apiKey: '', // 已填写实际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();
}

最小复现代码

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'));
  }
}

有没有大佬遇到过类似的问题?明明数据很快就返回了,但超时异常还是会触发,而且try-catch还抓不到,实在搞不懂哪里出问题了😭

内容来源于stack exchange

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.04.08 10:58:07