PMU/lib/data/controllers/SearchController.dart
2024-12-19 08:53:16 +04:00

36 lines
1.0 KiB
Dart
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import 'package:dio/dio.dart';
import 'package:flutter/cupertino.dart';
class SearchController with ChangeNotifier {
final Dio _dio = Dio();
List<dynamic> _results = [];
String? _query;
List<dynamic> get results => _results;
Future<void> search(String? q) async {
_query = q;
if (_query != null && _query!.isNotEmpty) {
try {
final Response<dynamic> response = await _dio.get<Map<dynamic, dynamic>>(
'https://api.potterdb.com/v1/characters',
queryParameters: {'filter[name_const]': _query},
);
// Обработка полученных данных
if (response.data != null && response.data['data'] != null) {
_results = response.data['data'];
} else {
_results = [];
}
} catch (e) {
print('Error fetching data: $e');
_results = [];
}
} else {
_results = [];
}
notifyListeners(); // Уведомляем слушателей об изменении данных
}
}