Skip to content

Commit 2105b47

Browse files
authored
Merge pull request #1 from Radlet/http
http code added
2 parents b89f167 + 7d3dce2 commit 2105b47

File tree

6 files changed

+50
-15
lines changed

6 files changed

+50
-15
lines changed

lib/routes.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ class Routes {
2929
body1: TextStyle(fontSize: 14.0, fontFamily: 'Hind'),
3030
),
3131
),
32-
initialRoute: '/',
32+
initialRoute: '/discovery',
3333
routes: routes));
3434
}
3535
}

lib/screens/Discovery/Discovery.dart

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import 'package:flutter/material.dart';
22
import './widgets/DeviceListItem.dart';
3+
import '../../util/apis/discovery.dart';
4+
import '../../util/data/Device.dart';
35

46
class Discovery extends StatefulWidget {
57
const Discovery({
@@ -16,18 +18,21 @@ class Discovery extends StatefulWidget {
1618
}
1719

1820
class DiscoveryState extends State<Discovery> {
19-
List<Map<String, String>> devices;
21+
List<Device> devices;
2022

2123
@override
2224
void initState() {
23-
devices = new List<Map<String, String>>();
24-
devices.add({"id": "A"});
25-
devices.add({"id": "B"});
26-
devices.add({"id": "C"});
25+
devices = new List<Device>();
2726
super.initState();
27+
28+
getDiscoveredList().then((fetchedDevices) {
29+
updateDeviceList(fetchedDevices);
30+
}).catchError((onError) {
31+
print("Error fetching device list");
32+
});
2833
}
2934

30-
void updateDeviceList(List<Map<String, String>> newDeviceList) {
35+
void updateDeviceList(List<Device> newDeviceList) {
3136
// TODO: Add a compasion mechanism
3237
setState(() {
3338
devices = newDeviceList;
@@ -42,7 +47,6 @@ class DiscoveryState extends State<Discovery> {
4247

4348
@override
4449
Widget build(BuildContext context) {
45-
4650
return Scaffold(
4751
appBar: AppBar(
4852
title: const Text("New devices"),
@@ -51,7 +55,7 @@ class DiscoveryState extends State<Discovery> {
5155
child: new ListView.builder(
5256
itemCount: devices.length,
5357
itemBuilder: (BuildContext context, int index) {
54-
return new DeviceListItem(deviceID: devices[index]["id"]);
58+
return new DeviceListItem(deviceID: devices[index].id);
5559
})),
5660
);
5761
}

lib/util/apis/discovery.dart

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,25 @@
1+
import 'dart:convert';
2+
13
import './helper.dart' as helper;
24
import './urls.dart' as urls;
5+
import '../data/Device.dart';
6+
7+
Future<List<Device>> getDiscoveredList([Map<String, String> data = const {}]) {
8+
return helper.makeGetRequest(urls.DISCOVERY_ENDPOINT, data).then((response) {
9+
if( response.statusCode != 200 ){
10+
// Server responded with error code
11+
throw Exception();
12+
}
13+
14+
var parsedJson = jsonDecode(response.body);
315

4-
Future getDiscoveredList( Map<String,String> data ){
5-
return helper.makePostRequest(urls.DISCOVERY_URL, data);
6-
}
16+
List<Device> devices = new List<Device>();
17+
for (var item in parsedJson["deviceArray"]) {
18+
devices.add(new Device(item["id"], item["name"]));
19+
}
20+
return devices;
21+
}).catchError((onError){
22+
// Connection failed
23+
throw Exception();
24+
});
25+
}

lib/util/apis/helper.dart

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
import 'package:http/http.dart' as http;
2+
import './urls.dart';
23

3-
Future makePostRequest(String url, Map<String, String> data) {
4-
return http.post(url, body: data);
4+
Future makePostRequest(String endpoint, Map<String, String> data) {
5+
return http.post(new Uri.http(SERVER_URL,endpoint), body: data);
6+
}
7+
8+
Future makeGetRequest(String endpoint, Map<String, String> data) {
9+
return http.get(new Uri.http(SERVER_URL,endpoint));
510
}

lib/util/apis/urls.dart

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
const String DISCOVERY_URL = "http://xyyz.com";
1+
const SERVER_URL = "192.168.100.15:8080";
2+
const String DISCOVERY_ENDPOINT = "/getavailabledevices";

lib/util/data/Device.dart

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
class Device {
2+
Device(this.id, this.name);
3+
4+
final String id;
5+
final String name;
6+
}

0 commit comments

Comments
 (0)