-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfavourite_service
More file actions
85 lines (72 loc) · 2.71 KB
/
Copy pathfavourite_service
File metadata and controls
85 lines (72 loc) · 2.71 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import 'dart:convert';
import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:http/http.dart' as http;
import '../domain/account/account_model.dart';
import '../domain/account/favourite_model.dart';
class DropdownValue extends StateNotifier<List<UserDataModel>> {
DropdownValue() : super([]);
String userUrl = 'https://reqres.in/api/users?page=2';
Future<List<UserDataModel>> getUserData() async {
try {
final response = await http.get(Uri.parse(userUrl));
print('Response Body: ${response.body}');
if (response.statusCode == 200) {
print('Status Code: ${response.statusCode}');
final jsonData = json.decode(response.body);
final users = jsonData['data'] as List<dynamic>;
return users.map((user) {
return UserDataModel(
first_name: user['first_name'],
last_name: user['last_name'],
email: user['email'],
id:user['id'],
avatar: user['avatar'],
);
}).toList();
} else {
throw Exception('Failed to fetch user data');
}
} catch (e) {
throw Exception('Error: $e');
}
}
Future<List<FavouriteUserDataModel>> getFavouriteUser() async {
try {
final response = await http.get(Uri.parse(userUrl));
print('Response Body: ${response.body}');
if (response.statusCode == 200) {
print('Status Code: ${response.statusCode}');
final jsonData = json.decode(response.body);
final users = jsonData['data'] as List<dynamic>;
return users.map((user) {
return FavouriteUserDataModel(
first_name: user['first_name'],
last_name: user['last_name'],
avatar: user['avatar'],
);
}).toList();
} else {
throw Exception('Failed to fetch user data');
}
} catch (e) {
throw Exception('Error: $e');
}
}
}
final dropdownValueNotifierProvider = StateNotifierProvider<DropdownValue, List<UserDataModel>>(
(ref) => DropdownValue(),
);
final accountListFutureProvider = FutureProvider<List<UserDataModel>>((ref) async {
final dropdownValue = ref.read(dropdownValueNotifierProvider.notifier);
return dropdownValue.getUserData();
});
final accountCategoryProvider = StateProvider<String>((ref) => 'Account');
//
// final favouriteListFutureProvider =
// FutureProvider<List<FavouriteUserDataModel>>((ref) async {
// final dropdownValue = ref.read(dropdownValueNotifierProvider.notifier);
// return dropdownValue.getFavouriteUser();
// });
// final favouritesCategoryProvider = StateProvider<String>((ref) => 'Favourites');
///makes the initial value as Accounts
final accountCategory = StateProvider<String>((ref) => 'All Accounts');