From cbc265e04b168d5ab503a30d4fc920d57538a16f Mon Sep 17 00:00:00 2001 From: Maximilian Betz <Maximilian.Betz@awi.de> Date: Thu, 3 Feb 2022 16:06:16 +0100 Subject: [PATCH] added first future http get, not working yet --- android/app/src/main/AndroidManifest.xml | 2 + lib/configuration.dart | 58 +++++++++++++++++++++++- lib/datamodel.dart | 10 ++++ pubspec.lock | 14 ++++++ pubspec.yaml | 1 + 5 files changed, 83 insertions(+), 2 deletions(-) diff --git a/android/app/src/main/AndroidManifest.xml b/android/app/src/main/AndroidManifest.xml index 1574dd3..28156ba 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 15106e3..68b83cf 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 04c07e9..8e9b1e8 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 b87e8a0..4fae244 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 e2f48a4..60438c9 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. -- GitLab