Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PROG2900
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Sara Savanovic Djordjevic
PROG2900
Commits
4238877c
Commit
4238877c
authored
1 year ago
by
Sara Savanovic Djordjevic
Browse files
Options
Downloads
Patches
Plain Diff
update: app-server connection
parent
9d871fbf
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
app/lib/main.dart
+34
-21
34 additions, 21 deletions
app/lib/main.dart
server/__pycache__/consts.cpython-311.pyc
+0
-0
0 additions, 0 deletions
server/__pycache__/consts.cpython-311.pyc
with
34 additions
and
21 deletions
app/lib/main.dart
+
34
−
21
View file @
4238877c
...
...
@@ -2,13 +2,32 @@ import 'package:flutter/material.dart';
import
'package:http/http.dart'
as
http
;
import
'dart:convert'
;
import
'dart:async'
;
import
'dart:io'
;
const
serverURI
=
"https://127.0.0.1:
8443"
;
const
mapEndpoint
=
serverURI
+
"
/update_map
"
;
const
fetchInterval
=
5
;
const
String
port
=
"
8443"
;
const
String
serverURI
=
"
https://127.0.0.1:
$port
/
"
;
const
int
fetchInterval
=
5
;
void
main
()
{
runApp
(
const
App
());
Future
<
void
>
main
()
async
{
// NB temporary test print
print
(
serverURI
);
try
{
// Create a custom HTTP client with disabled SSL certificate validation
HttpClient
client
=
HttpClient
()
.
.
badCertificateCallback
=
(
X509Certificate
cert
,
String
host
,
int
port
)
=
>
true
;
// Use the custom HTTP client for requests
var
request
=
await
client
.
getUrl
(
Uri
.
parse
(
serverURI
));
var
response
=
await
request
.
close
();
// Handle response
print
(
'Response status:
${response.statusCode}
'
);
}
catch
(
e
)
{
// Handle connection error
print
(
'Failed to connect to the server:
$e
'
);
}
}
class
App
extends
StatelessWidget
{
...
...
@@ -22,8 +41,9 @@ class App extends StatelessWidget {
}
}
// fetchData requests data from the update_map endpoint
Future
<
void
>
fetchData
()
async
{
final
response
=
await
http
.
get
(
Uri
.
parse
(
mapEndpoint
));
final
response
=
await
http
.
get
(
Uri
.
parse
(
serverURI
));
if
(
response
.
statusCode
==
200
)
{
// Parse the JSON response
...
...
@@ -31,6 +51,7 @@ Future<void> fetchData() async {
// NB temporary test print
print
(
data
);
print
(
"Test print, fetchData triggered"
);
}
else
{
// Handle the error case
print
(
'Failed to fetch data. Status code:
${response.statusCode}
'
);
}
...
...
@@ -52,7 +73,7 @@ class _DefaultPageState extends State<DefaultPage> {
// Call fetchData when the widget is first created
fetchData
();
// Schedule fetchData to run periodically based on
FETCH_INTERVAL
const
// Schedule fetchData to run periodically based on
fetchInterval
const
const
Duration
fiveMinutes
=
Duration
(
minutes:
fetchInterval
);
_timer
=
Timer
.
periodic
(
fiveMinutes
,
(
timer
)
{
fetchData
();
...
...
@@ -61,7 +82,7 @@ class _DefaultPageState extends State<DefaultPage> {
@override
void
dispose
()
{
// Cancel
the
timer
when the widget is disposed to avoid memory leaks
// Cancel timer
on widget termination
_timer
.
cancel
();
super
.
dispose
();
}
...
...
@@ -70,25 +91,17 @@ class _DefaultPageState extends State<DefaultPage> {
Widget
build
(
BuildContext
context
)
{
return
Scaffold
(
appBar:
AppBar
(
title:
const
Text
(
'
Flutter Mobile Ap
p'
),
title:
const
Text
(
'
IceMa
p'
),
),
body:
Center
(
body:
const
Center
(
child:
Column
(
mainAxisAlignment:
MainAxisAlignment
.
center
,
children:
<
Widget
>[
const
Text
(
'Default
flutter app
page
!
'
,
Text
(
'Default page'
,
style:
TextStyle
(
fontSize:
24
),
),
const
SizedBox
(
height:
20
),
ElevatedButton
(
onPressed:
()
async
{
// 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'
),
),
SizedBox
(
height:
20
),
],
),
),
...
...
This diff is collapsed.
Click to expand it.
server/__pycache__/consts.cpython-311.pyc
+
0
−
0
View file @
4238877c
No preview for this file type
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment