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';
print(client.options.connectTimeout);