outsourced httpRequest method
This commit is contained in:
4
Todo.txt
4
Todo.txt
@@ -1,14 +1,14 @@
|
|||||||
todo:
|
todo:
|
||||||
fix send file to server
|
|
||||||
add feedback to send to server function
|
add feedback to send to server function
|
||||||
im englischen abändern
|
im englischen abändern
|
||||||
|
|
||||||
keine ahnung obs funktioniert:
|
keine ahnung obs funktioniert:
|
||||||
TEST SEND FILE TO SERVER FUNCTION
|
|
||||||
eintrg in db wenn http response (in sent column)
|
eintrg in db wenn http response (in sent column)
|
||||||
|
|
||||||
|
|
||||||
not to do:
|
not to do:
|
||||||
|
TEST SEND FILE TO SERVER FUNCTION
|
||||||
|
fix send file to server
|
||||||
Platzierung garnix als standart aber pflichtfeld
|
Platzierung garnix als standart aber pflichtfeld
|
||||||
sandorte ansehen in karte (extra tab)
|
sandorte ansehen in karte (extra tab)
|
||||||
einzelnen eintrag löschen
|
einzelnen eintrag löschen
|
||||||
|
|||||||
@@ -3,13 +3,13 @@ import 'dart:convert';
|
|||||||
import 'dart:io';
|
import 'dart:io';
|
||||||
import 'package:fforte/addCam/cam_widgets.dart';
|
import 'package:fforte/addCam/cam_widgets.dart';
|
||||||
import 'package:fforte/other/db_helper.dart';
|
import 'package:fforte/other/db_helper.dart';
|
||||||
|
import 'package:fforte/other/methods.dart';
|
||||||
import 'package:file_picker/file_picker.dart';
|
import 'package:file_picker/file_picker.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||||
import 'package:geolocator/geolocator.dart';
|
import 'package:geolocator/geolocator.dart';
|
||||||
import 'package:latlong2/latlong.dart';
|
import 'package:latlong2/latlong.dart';
|
||||||
import 'package:animations/animations.dart';
|
import 'package:animations/animations.dart';
|
||||||
import 'package:dio/dio.dart';
|
|
||||||
import 'package:shared_preferences/shared_preferences.dart';
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
class AddCamMain extends StatefulWidget {
|
class AddCamMain extends StatefulWidget {
|
||||||
@@ -288,40 +288,12 @@ class _AddCamMainState extends State<AddCamMain> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
void _httpRequest() async {
|
void _httpRequest() async {
|
||||||
Map<String, dynamic> place = getPlace();
|
Map<String, dynamic> place = getPlace();
|
||||||
|
|
||||||
// print(jsonEncode(place));
|
Methods(jsonEncode(place)).httpRequest();
|
||||||
|
|
||||||
final dio = Dio();
|
|
||||||
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
|
||||||
|
|
||||||
dio.options.responseType = ResponseType.plain;
|
|
||||||
Response response =
|
|
||||||
Response(requestOptions: RequestOptions(path: ''), statusCode: 400);
|
|
||||||
|
|
||||||
try {
|
|
||||||
response = await dio.post(prefs.getString('apiAddress') ?? "",
|
|
||||||
data: jsonEncode(place));
|
|
||||||
print(jsonEncode(place));
|
|
||||||
} on DioException catch (e) {
|
|
||||||
if (e.response?.statusCode == 500) {
|
|
||||||
print('-------------------------');
|
|
||||||
print('code 500');
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
if (response.statusCode == 201) {
|
|
||||||
print("------------------");
|
|
||||||
print(response.statusCode);
|
|
||||||
var placeDB = DBHelper();
|
|
||||||
|
|
||||||
await placeDB.updateSent();
|
|
||||||
} else {
|
|
||||||
print("----------------");
|
|
||||||
print("Anderer code");
|
|
||||||
print(response.statusCode);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<void> saveFile() async {
|
Future<void> saveFile() async {
|
||||||
|
|||||||
@@ -1,3 +1,6 @@
|
|||||||
|
import 'dart:io';
|
||||||
|
import 'package:fforte/other/methods.dart';
|
||||||
|
import 'package:file_picker/file_picker.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
import 'package:flutter_gen/gen_l10n/app_localizations.dart';
|
||||||
import 'addCam/add_cam_main.dart';
|
import 'addCam/add_cam_main.dart';
|
||||||
@@ -8,6 +11,18 @@ import 'addCam/add_cam_main.dart';
|
|||||||
class HomePage extends StatelessWidget {
|
class HomePage extends StatelessWidget {
|
||||||
const HomePage({super.key});
|
const HomePage({super.key});
|
||||||
|
|
||||||
|
void _sendFile() async {
|
||||||
|
FilePickerResult? result = await FilePicker.platform.pickFiles();
|
||||||
|
|
||||||
|
if (result != null) {
|
||||||
|
File file = File(result.files.single.path!);
|
||||||
|
String content = await file.readAsString();
|
||||||
|
|
||||||
|
Methods(content).httpRequest();
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@override
|
@override
|
||||||
@@ -47,7 +62,9 @@ class HomePage extends StatelessWidget {
|
|||||||
child: Text(AppLocalizations.of(context)!.addplace)),
|
child: Text(AppLocalizations.of(context)!.addplace)),
|
||||||
ElevatedButton(onPressed: () => Navigator.pushNamed(context, '/viewCams'),
|
ElevatedButton(onPressed: () => Navigator.pushNamed(context, '/viewCams'),
|
||||||
child: Text(AppLocalizations.of(context)!.viewplaces)),
|
child: Text(AppLocalizations.of(context)!.viewplaces)),
|
||||||
ElevatedButton(onPressed: () {}, child: Text(AppLocalizations.of(context)!.sendfile))
|
ElevatedButton(onPressed: () {
|
||||||
|
_sendFile();
|
||||||
|
}, child: Text(AppLocalizations.of(context)!.sendfile))
|
||||||
],
|
],
|
||||||
)),
|
)),
|
||||||
],
|
],
|
||||||
|
|||||||
45
lib/other/methods.dart
Normal file
45
lib/other/methods.dart
Normal file
@@ -0,0 +1,45 @@
|
|||||||
|
import 'dart:convert';
|
||||||
|
|
||||||
|
import 'package:dio/dio.dart';
|
||||||
|
import 'package:fforte/other/db_helper.dart';
|
||||||
|
import 'package:shared_preferences/shared_preferences.dart';
|
||||||
|
|
||||||
|
class Methods {
|
||||||
|
String httpData;
|
||||||
|
|
||||||
|
Methods(this.httpData);
|
||||||
|
|
||||||
|
void httpRequest() async {
|
||||||
|
// print(jsonEncode(place));
|
||||||
|
|
||||||
|
final dio = Dio();
|
||||||
|
final SharedPreferences prefs = await SharedPreferences.getInstance();
|
||||||
|
|
||||||
|
dio.options.responseType = ResponseType.plain;
|
||||||
|
Response response =
|
||||||
|
Response(requestOptions: RequestOptions(path: ''), statusCode: 400);
|
||||||
|
|
||||||
|
try {
|
||||||
|
response = await dio.post(prefs.getString('apiAddress') ?? "",
|
||||||
|
data: jsonEncode(httpData));
|
||||||
|
// print(jsonEncode(httpData));
|
||||||
|
} on DioException catch (e) {
|
||||||
|
if (e.response?.statusCode == 500) {
|
||||||
|
print('-------------------------');
|
||||||
|
print('code 500');
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (response.statusCode == 201) {
|
||||||
|
print("------------------");
|
||||||
|
print(response.statusCode);
|
||||||
|
var placeDB = DBHelper();
|
||||||
|
|
||||||
|
await placeDB.updateSent();
|
||||||
|
} else {
|
||||||
|
print("----------------");
|
||||||
|
print("Anderer code");
|
||||||
|
print(response.statusCode);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user