From 86496005e02237a8549b957c9259174e705aeff4 Mon Sep 17 00:00:00 2001 From: marco Date: Fri, 24 Jan 2025 17:41:24 +0100 Subject: [PATCH] Very simple api request --- analysis_options.yaml | 3 ++- lib/main.dart | 3 +++ lib/model/flood_station.dart | 17 +++++++++++++++++ lib/services/api.dart | 20 ++++++++++++++++++++ pubspec.yaml | 2 ++ 5 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 lib/model/flood_station.dart create mode 100644 lib/services/api.dart diff --git a/analysis_options.yaml b/analysis_options.yaml index 0d29021..7a178e6 100644 --- a/analysis_options.yaml +++ b/analysis_options.yaml @@ -22,7 +22,8 @@ linter: # producing the lint. rules: # avoid_print: false # Uncomment to disable the `avoid_print` rule - # prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule + prefer_single_quotes: true # Uncomment to enable the `prefer_single_quotes` rule + prefer_relative_imports: true # Additional information about this file can be found at # https://dart.dev/guides/language/analysis-options diff --git a/lib/main.dart b/lib/main.dart index 9d002fe..afda00c 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,5 +1,7 @@ import 'package:flutter/material.dart'; +import 'services/api.dart'; + void main() { runApp(const MyApp()); } @@ -9,6 +11,7 @@ class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { + Api.fetchStations(); return MaterialApp( title: 'Floodwatch', theme: ThemeData( diff --git a/lib/model/flood_station.dart b/lib/model/flood_station.dart new file mode 100644 index 0000000..06a78c1 --- /dev/null +++ b/lib/model/flood_station.dart @@ -0,0 +1,17 @@ +class FloodStation { + final String id; + final String town; + final double? latestReading; + + FloodStation({ + required this.id, + required this.town, + this.latestReading, + }); + + factory FloodStation.fromMap(Map json) => FloodStation( + id: json['@id'] ?? '', + town: json['town'] ?? '', + latestReading: double.tryParse(json['latestReading']?.toString() ?? ''), + ); +} diff --git a/lib/services/api.dart b/lib/services/api.dart new file mode 100644 index 0000000..afb7a43 --- /dev/null +++ b/lib/services/api.dart @@ -0,0 +1,20 @@ +import 'package:http/http.dart' as http; +import 'dart:convert'; + +import '../model/flood_station.dart'; + +class Api { + static const String _rootUrl = + 'https://environment.data.gov.uk/flood-monitoring'; + + static Future fetchStations() async { + List stations = []; + final response = await http.get(Uri.parse('$_rootUrl/id/stations')); + if (response.statusCode == 200) { + final Map jsonStr = jsonDecode(response.body); + for (final str in jsonStr['items']) { + stations.add(FloodStation.fromMap(str)); + } + } + } +} diff --git a/pubspec.yaml b/pubspec.yaml index 709b672..b56a10f 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -11,6 +11,8 @@ dependencies: sdk: flutter cupertino_icons: ^1.0.8 + fl_chart: ^0.70.2 + http: ^1.3.0 dev_dependencies: flutter_test: