Skip to content
Snippets Groups Projects
Commit 9d871fbf authored by Sara Savanovic Djordjevic's avatar Sara Savanovic Djordjevic
Browse files

update: connect app to update_map endpoint

parent 8a2e2b5f
No related branches found
No related tags found
No related merge requests found
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import 'dart:async';
const SERVER_URI = "https://127.0.0.1:8443";
const serverURI = "https://127.0.0.1:8443";
const mapEndpoint = serverURI + "/update_map";
const fetchInterval = 5;
void main() {
runApp(const App());
......@@ -18,9 +22,49 @@ class App extends StatelessWidget {
}
}
// Default page
class DefaultPage extends StatelessWidget {
const DefaultPage({super.key});
Future<void> fetchData() async {
final response = await http.get(Uri.parse(mapEndpoint));
if (response.statusCode == 200) {
// Parse the JSON response
Map<String, dynamic> data = jsonDecode(response.body);
// NB temporary test print
print(data);
} else { // Handle the error case
print('Failed to fetch data. Status code: ${response.statusCode}');
}
}
class DefaultPage extends StatefulWidget {
const DefaultPage({Key? key}) : super(key: key);
@override
_DefaultPageState createState() => _DefaultPageState();
}
class _DefaultPageState extends State<DefaultPage> {
late Timer _timer;
@override
void initState() {
super.initState();
// Call fetchData when the widget is first created
fetchData();
// Schedule fetchData to run periodically based on FETCH_INTERVAL const
const Duration fiveMinutes = Duration(minutes: fetchInterval);
_timer = Timer.periodic(fiveMinutes, (timer) {
fetchData();
});
}
@override
void dispose() {
// Cancel the timer when the widget is disposed to avoid memory leaks
_timer.cancel();
super.dispose();
}
@override
Widget build(BuildContext context) {
......@@ -39,9 +83,8 @@ class DefaultPage extends StatelessWidget {
const SizedBox(height: 20),
ElevatedButton(
onPressed: () async {
// Attempt to connect to server
var response = await http.get(Uri.parse(SERVER_URI));
// Attempt to connect to the server
var response = await http.get(Uri.parse(serverURI));
print('Response from server: ${response.body}');
},
child: const Text('Some text'),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment