diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 1574dd3937e564ec7c0fbcff50f08a5488065c83..28156baaab9de16fa55fd651a0be37820954df5d 100644 --- a/android/app/src/main/AndroidManifest.xml +++ b/android/app/src/main/AndroidManifest.xml @@ -31,4 +31,6 @@ android:name="flutterEmbedding" android:value="2" /> </application> + <!-- Required to fetch data from the internet. --> + <uses-permission android:name="android.permission.INTERNET" /> </manifest> diff --git a/lib/configuration.dart b/lib/configuration.dart index 15106e35c0fc2a91d1cb77c8583ec015d7cd0efb..68b83cf79a093d5a49666f6048ec54f858d349f9 100644 --- a/lib/configuration.dart +++ b/lib/configuration.dart @@ -1,5 +1,44 @@ import 'package:flutter/cupertino.dart'; import 'package:flutter/material.dart'; +import 'package:http/http.dart' as http; +import 'dart:convert'; + +class Collection { + final int id; + final String description; + final String collectionName; + + const Collection({ + required this.id, + required this.description, + required this.collectionName, + }); + + factory Collection.fromJson(Map<String, dynamic> json) { + return Collection( + id: json['id'], + collectionName: json['collectionName'], + description: json['description'], + ); + } +} + +Future<Collection> fetchCollection() async { + print("Start HTTP GET++##"); + + final response = await http + .get(Uri.parse('https://sensor.awi.de/rest/sensors/collections/getCollection/22')); + + if (response.statusCode == 200) { + // If the server did return a 200 OK response, + // then parse the JSON. + return Collection.fromJson(jsonDecode(response.body)); + } else { + // If the server did not return a 200 OK response, + // then throw an exception. + throw Exception('Failed to load Collection'); + } +} class Configuration extends StatefulWidget { @@ -18,12 +57,16 @@ class _MyHomePageState extends State<Configuration> { '100', ]; - late Future<String> eventTypes; + + late Future<Collection> futureCollection; @override void initState() { super.initState(); + futureCollection = fetchCollection(); } + late Future<String> eventTypes; + void _incrementCounter() { setState(() { @@ -80,7 +123,18 @@ class _MyHomePageState extends State<Configuration> { }); }, ), - Text('blabla'), + FutureBuilder<Collection>( + future: futureCollection, + builder: (context, snapshot){ + if (snapshot.hasData) + { + return Text(snapshot.data!.collectionName); + } + else{ + return CircularProgressIndicator(); + } + } + ), ], ), ), diff --git a/lib/datamodel.dart b/lib/datamodel.dart index 04c07e96c2228919d3a96cf837bea0ccabdc8b4f..8e9b1e8b16593cc66a411d93500fa7516c19cbb1 100644 --- a/lib/datamodel.dart +++ b/lib/datamodel.dart @@ -83,6 +83,7 @@ abstract class DeviceStoreBase { abstract class EventStoreBase{ List<Event> store = []; + } abstract class EventCurrentBase{ @@ -91,6 +92,15 @@ abstract class EventCurrentBase{ abstract class EventTypeStoreBase{ List<EventType> store = []; + + int getEventIdFromName(String name) { + for (var eventType in store) { + if (eventType.name == name) { + return eventType.id; + } + } + throw Exception('Event with name :' + name + ' was not found.'); + } } class EventStoreInstance extends EventStoreBase { diff --git a/pubspec.lock b/pubspec.lock index b87e8a0c564983a5998561760ef2fe7aa14075a2..4fae244ebe64b5c9b145c801bddfc697fc9310c3 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -93,6 +93,20 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + url: "https://pub.dartlang.org" + source: hosted + version: "0.13.4" + http_parser: + dependency: transitive + description: + name: http_parser + url: "https://pub.dartlang.org" + source: hosted + version: "4.0.0" js: dependency: transitive description: diff --git a/pubspec.yaml b/pubspec.yaml index e2f48a48f6cbe630d60fdf9d54bc815db413311b..60438c989cc08d56d04a5ad5b5257092538b6e14 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -31,6 +31,7 @@ dependencies: sdk: flutter shared_preferences: ^2.0.12 + http: ^0.13.4 # The following adds the Cupertino Icons font to your application.