Very simple api request
This commit is contained in:
@@ -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(
|
||||
|
||||
17
lib/model/flood_station.dart
Normal file
17
lib/model/flood_station.dart
Normal 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
20
lib/services/api.dart
Normal 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));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user