diff --git a/.gitignore b/.gitignore index 3820a95..0865105 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,5 @@ +.env + # Miscellaneous *.class *.log diff --git a/lib/api_handler.dart b/lib/api_handler.dart new file mode 100644 index 0000000..fe24bdf --- /dev/null +++ b/lib/api_handler.dart @@ -0,0 +1,36 @@ +import 'package:http/http.dart' as http; +import 'package:flutter_dotenv/flutter_dotenv.dart'; + +import 'dart:core'; +import 'dart:convert'; + +import 'transport_helper.dart'; + +class ApiHandler { + // TODO: Split next line for better Error Handling + static final String baseUrl = dotenv.get("VBB_API_URL"); + static final String accessID = dotenv.get("VBB_API_KEY"); + + //static Future> findStations(String locationName) async { + static Future findStations(String locationName) async { + String urlPath = '/api/fahrinfo/latest/location.name'; + String urlParams = '?input=$locationName'; + Uri url = Uri.https(baseUrl, urlPath, { + 'accessId': accessID, + 'input': locationName, + 'maxNo': '2', + 'type': 'S' // Station/Stops only + }); + //print(url); + var jsonResponse = await http.get(url, + headers: { + 'Accept': 'application/json' + }); + final httpPackageJson = json.decode(jsonResponse.body) as Map; + //print(httpPackageJson["stopLocationOrCoordLocation"]); + final id = httpPackageJson["stopLocationOrCoordLocation"][0]["StopLocation"]["id"]; + //print("and now just ids: "); + //print(id); + return id; + } +} diff --git a/lib/main.dart b/lib/main.dart index 244a702..9117496 100644 --- a/lib/main.dart +++ b/lib/main.dart @@ -1,6 +1,9 @@ import 'package:flutter/material.dart'; +import 'package:flutter_dotenv/flutter_dotenv.dart'; +import 'api_handler.dart'; -void main() { +void main() async { + await dotenv.load(); runApp(const MyApp()); } @@ -55,6 +58,10 @@ class MyHomePage extends StatefulWidget { class _MyHomePageState extends State { int _counter = 0; + //String API_KEY = ApiHandler.accessID; + String API_KEY = "no telly"; + //Future> ?id; + String id = ''; void _incrementCounter() { setState(() { @@ -66,6 +73,10 @@ class _MyHomePageState extends State { _counter++; }); } + void show_result(Future future_id) async { + id = await future_id; + setState(() { }); + } @override Widget build(BuildContext context) { @@ -109,6 +120,13 @@ class _MyHomePageState extends State { '$_counter', style: Theme.of(context).textTheme.headlineMedium, ), + Text('API key from ENV is $API_KEY'), + MaterialButton(onPressed: () { show_result(ApiHandler.findStations('S Biesdorf')); setState(() {}); }, + child: Text( + 'Search for \'S Biesdorf\'' + ), + ), + Text(id) ], ), ), diff --git a/pubspec.lock b/pubspec.lock index 438acd6..ede043e 100644 --- a/pubspec.lock +++ b/pubspec.lock @@ -62,6 +62,14 @@ packages: description: flutter source: sdk version: "0.0.0" + flutter_dotenv: + dependency: "direct main" + description: + name: flutter_dotenv + sha256: d41da11fb497314fbf89811ec30af02d1d898b47980a129f0a8c0a1720460ba2 + url: "https://pub.dev" + source: hosted + version: "6.0.1" flutter_lints: dependency: "direct dev" description: @@ -75,6 +83,22 @@ packages: description: flutter source: sdk version: "0.0.0" + http: + dependency: "direct main" + description: + name: http + sha256: "87721a4a50b19c7f1d49001e51409bddc46303966ce89a65af4f4e6004896412" + url: "https://pub.dev" + source: hosted + version: "1.6.0" + http_parser: + dependency: transitive + description: + name: http_parser + sha256: "178d74305e7866013777bab2c3d8726205dc5a4dd935297175b19a23a2e66571" + url: "https://pub.dev" + source: hosted + version: "4.1.2" leak_tracker: dependency: transitive description: @@ -192,6 +216,14 @@ packages: url: "https://pub.dev" source: hosted version: "0.7.10" + typed_data: + dependency: transitive + description: + name: typed_data + sha256: f9049c039ebfeb4cf7a7104a675823cd72dba8297f264b6637062516699fa006 + url: "https://pub.dev" + source: hosted + version: "1.4.0" vector_math: dependency: transitive description: @@ -204,10 +236,18 @@ packages: dependency: transitive description: name: vm_service - sha256: "046d3928e16fa4dc46e8350415661755ab759d9fc97fc21b5ab295f71e4f0499" + sha256: "0016aef94fc66495ac78af5859181e3f3bf2026bd8eecc72b9565601e19ab360" url: "https://pub.dev" source: hosted - version: "15.1.0" + version: "15.2.0" + web: + dependency: transitive + description: + name: web + sha256: "868d88a33d8a87b18ffc05f9f030ba328ffefba92d6c127917a2ba740f9cfe4a" + url: "https://pub.dev" + source: hosted + version: "1.1.1" sdks: dart: ">=3.11.5 <4.0.0" flutter: ">=3.18.0-18.0.pre.54" diff --git a/pubspec.yaml b/pubspec.yaml index fa008d0..67d7205 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -34,6 +34,8 @@ dependencies: # The following adds the Cupertino Icons font to your application. # Use with the CupertinoIcons class for iOS style icons. cupertino_icons: ^1.0.8 + flutter_dotenv: ^6.0.1 + http: ^1.6.0 dev_dependencies: flutter_test: @@ -61,6 +63,8 @@ flutter: # assets: # - images/a_dot_burr.jpeg # - images/a_dot_ham.jpeg + assets: + - .env # An image asset can refer to one or more resolution-specific "variants", see # https://flutter.dev/to/resolution-aware-images