geo-smart-app/lib/service/position_service.dart

52 lines
1.3 KiB
Dart
Raw Normal View History

2019-12-16 00:18:09 +00:00
import 'package:dio/dio.dart';
import 'package:geosmart/model/position.dart';
import 'package:geosmart/model/response.dart';
import 'package:geosmart/service/setting_service.dart';
2019-12-16 00:18:09 +00:00
class PositionService {
2019-12-16 00:18:09 +00:00
final Dio _dio = Dio();
final SettingService _settingBloc = SettingService();
2019-12-16 00:18:09 +00:00
PositionService();
2019-12-16 00:18:09 +00:00
Future<ResponseModel> sendPosition(String lat, String lng) async {
var m = await _settingBloc.getSetting();
2019-12-16 00:18:09 +00:00
Position position = new Position(
id: m.id,
2019-12-16 00:18:09 +00:00
type: "user",
lat: lat,
lng: lng,
);
try {
Response response = await _dio.post(
m.host + "/point/set",
2019-12-16 00:18:09 +00:00
data: position.toJson(),
);
return ResponseModel.fromJson(response.data);
} on DioError catch (e) {
2020-01-03 23:36:33 +00:00
print(e.response);
if (e.response != null) {
return ResponseModel.fromJson(e.response.data);
} else {
return ResponseModel.fromNull();
}
2019-12-16 00:18:09 +00:00
}
}
Future<ResponseModel> stopTracking() async {
var m = await _settingBloc.getSetting();
2019-12-16 00:18:09 +00:00
try {
Response response = await _dio.post(
m.host + "/point/unset",
2019-12-16 00:18:09 +00:00
data: {
"id": m.id,
2019-12-16 00:18:09 +00:00
"type": "user",
},
);
return ResponseModel.fromJson(response.data);
} on DioError catch (e) {
return ResponseModel.fromJson(e.response.data);
}
}
}