Very simple api request

This commit is contained in:
2025-01-24 17:41:24 +01:00
parent 123136f265
commit 86496005e0
5 changed files with 44 additions and 1 deletions

View File

@@ -22,7 +22,8 @@ linter:
# producing the lint. # producing the lint.
rules: rules:
# avoid_print: false # Uncomment to disable the `avoid_print` rule # 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 # Additional information about this file can be found at
# https://dart.dev/guides/language/analysis-options # https://dart.dev/guides/language/analysis-options

View File

@@ -1,5 +1,7 @@
import 'package:flutter/material.dart'; import 'package:flutter/material.dart';
import 'services/api.dart';
void main() { void main() {
runApp(const MyApp()); runApp(const MyApp());
} }
@@ -9,6 +11,7 @@ class MyApp extends StatelessWidget {
@override @override
Widget build(BuildContext context) { Widget build(BuildContext context) {
Api.fetchStations();
return MaterialApp( return MaterialApp(
title: 'Floodwatch', title: 'Floodwatch',
theme: ThemeData( theme: ThemeData(

View File

@@ -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<String, dynamic> json) => FloodStation(
id: json['@id'] ?? '',
town: json['town'] ?? '',
latestReading: double.tryParse(json['latestReading']?.toString() ?? ''),
);
}

20
lib/services/api.dart Normal file
View File

@@ -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<void> fetchStations() async {
List<FloodStation> stations = [];
final response = await http.get(Uri.parse('$_rootUrl/id/stations'));
if (response.statusCode == 200) {
final Map<String, dynamic> jsonStr = jsonDecode(response.body);
for (final str in jsonStr['items']) {
stations.add(FloodStation.fromMap(str));
}
}
}
}

View File

@@ -11,6 +11,8 @@ dependencies:
sdk: flutter sdk: flutter
cupertino_icons: ^1.0.8 cupertino_icons: ^1.0.8
fl_chart: ^0.70.2
http: ^1.3.0
dev_dependencies: dev_dependencies:
flutter_test: flutter_test: