Skip to content

Commit 574bac9

Browse files
committed
Filter users by city
1 parent ac36c44 commit 574bac9

File tree

1 file changed

+62
-25
lines changed

1 file changed

+62
-25
lines changed

lib/pages/home.dart

+62-25
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,10 @@ class Home extends StatefulWidget {
1010
}
1111

1212
class _HomeState extends State<Home> {
13-
final apiKey = "";
1413
final _controller = TextEditingController();
1514

15+
String _query = "";
16+
1617
@override
1718
void dispose() {
1819
_controller.dispose();
@@ -35,35 +36,56 @@ class _HomeState extends State<Home> {
3536

3637
Future<List<User>> _future() async {
3738
final response = await http.get(
38-
Uri.parse('https://exercise-646d.restdb.io/rest/group-1'),
39+
_getUri(),
3940
headers: {
4041
'x-apikey': '5c5c7076f210985199db5488',
4142
},
4243
);
4344

4445
if (response.statusCode == 200) {
45-
return (json.decode(response.body) as List)
46+
final users = (json.decode(response.body) as List)
4647
.map((i) => User.fromJson(i))
4748
.toList();
49+
50+
return users;
4851
} else {
4952
throw Exception('Failed to load users');
5053
}
5154
}
5255

53-
Widget _body() {
54-
return FutureBuilder<List<User>>(
55-
future: _future(),
56-
builder: (context, snapshot) {
57-
if (snapshot.hasData) {
58-
return _content(snapshot.data);
59-
}
56+
Uri _getUri() {
57+
Uri uri;
6058

61-
if (snapshot.hasError) {
62-
return _showError(snapshot.error.toString());
63-
}
59+
if (_query.isEmpty) {
60+
uri = Uri(
61+
scheme: 'https',
62+
host: 'exercise-646d.restdb.io',
63+
path: 'rest/group-1',
64+
);
65+
} else {
66+
uri = Uri(
67+
scheme: 'https',
68+
host: 'exercise-646d.restdb.io',
69+
path: 'rest/group-1',
70+
queryParameters: {'q': '{"CITY":"$_query"}'},
71+
);
72+
}
6473

65-
return _showLoading();
66-
},
74+
return uri;
75+
}
76+
77+
Widget _body() {
78+
return Padding(
79+
padding: const EdgeInsets.all(8.0),
80+
child: Column(
81+
children: [_filterByCity(), _content()],
82+
),
83+
);
84+
}
85+
86+
Widget _showEmptyPrompt() {
87+
return Center(
88+
child: Text("No users in '$_query' found"),
6789
);
6890
}
6991

@@ -79,15 +101,28 @@ class _HomeState extends State<Home> {
79101
);
80102
}
81103

82-
Widget _content(List<User> users) {
83-
return Padding(
84-
padding: const EdgeInsets.all(8.0),
85-
child: Column(
86-
children: [
87-
_filterByCity(),
88-
Expanded(child: _listView(users)),
89-
],
90-
),
104+
Widget _content() {
105+
return FutureBuilder<List<User>>(
106+
future: _future(),
107+
builder: (context, snapshot) {
108+
if (snapshot.connectionState == ConnectionState.waiting) {
109+
return _showLoading();
110+
}
111+
112+
if (snapshot.hasData) {
113+
if (snapshot.data.isEmpty) {
114+
return _showEmptyPrompt();
115+
}
116+
117+
return Expanded(child: _listView(snapshot.data));
118+
}
119+
120+
if (snapshot.hasError) {
121+
return _showError(snapshot.error.toString());
122+
}
123+
124+
return _showLoading();
125+
},
91126
);
92127
}
93128

@@ -125,7 +160,9 @@ class _HomeState extends State<Home> {
125160
}
126161

127162
void _onFilterButtonPressed() {
128-
print("Button pressed");
163+
setState(() {
164+
_query = _controller.text;
165+
});
129166
}
130167

131168
Widget _listView(List<User> users) {

0 commit comments

Comments
 (0)