2024-12-10 12:59:08 +04:00
|
|
|
|
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(); // Уведомляем слушателей об изменении данных
|
|
|
|
|
}
|
2024-12-19 08:53:16 +04:00
|
|
|
|
}
|