Start device fetch

This commit is contained in:
hodasemi 2023-10-05 14:54:11 +02:00
parent b609f50ad6
commit c33455ebbc
2 changed files with 40 additions and 7 deletions

35
frontend/lib/devices.dart Normal file
View file

@ -0,0 +1,35 @@
import 'dart:async';
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
class Category {
final String name;
List<Device> devices;
Category(this.name) : devices = [];
static Future<List<Category>> fetch(String base_url) async {
final response = await http.get(Uri.parse("$base_url/devices"));
if (response.statusCode == 200) {
List<Category> categories = [];
Map<String, Map<String, dynamic>> json = jsonDecode(response.body);
return categories;
} else {
throw Exception("Failed to fetch devices");
}
}
}
class Device {
final int device_id;
bool led_state;
bool power_state;
double power_draw;
Device(this.device_id, this.led_state, this.power_state, this.power_draw);
}

View file

@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'devices.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
@ -105,13 +107,9 @@ class _MyHomePageState extends State<MyHomePage> {
// wireframe for each widget. // wireframe for each widget.
mainAxisAlignment: MainAxisAlignment.center, mainAxisAlignment: MainAxisAlignment.center,
children: <Widget>[ children: <Widget>[
const Text( Table(
'You have pushed the button this many times:', border: TableBorder.all(),
), )
Text(
'$_counter',
style: Theme.of(context).textTheme.headlineMedium,
),
], ],
), ),
), ),