app.dart 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. import 'dart:io';
  2. import 'package:farmer_client/models/entities/person.dart';
  3. import 'package:get/get.dart';
  4. import 'package:location/location.dart';
  5. import '../services/user.dart';
  6. import '../utils/location.dart';
  7. class AppController extends GetxController {
  8. // 有了这句, 可以直接 AppController.t 调用
  9. static AppController t = Get.find();
  10. final user = Rx<Person>(Person());
  11. final location = Rxn<LocationData>();
  12. final testInt = 1.obs;
  13. // get locationStr {
  14. // if(location.value!.longitude==null){
  15. // return null;
  16. //
  17. // }else{
  18. // return location.value!.longitude.toString() + "," + location.value!.latitude.toString();
  19. //
  20. // }
  21. // }
  22. @override
  23. void onInit() {
  24. super.onInit();
  25. // 尝试获取 location
  26. requireLocation().then((loc) async {
  27. var _data = await loc.getLocation();
  28. location(_data);
  29. // 监听位置变化
  30. loc.onLocationChanged.listen((LocationData currentLocation) {
  31. location(currentLocation);
  32. });
  33. }).catchError((e) {
  34. print(e);
  35. var message = e.message.toString().contains('CANCELED')
  36. ? '您取消了, 确定后将退出程序'
  37. : e.message;
  38. // Get.dialog();
  39. // Get.defaultDialog(
  40. // title: '提示',
  41. // middleText: message,
  42. // radius: 8,
  43. // textConfirm: '确定',
  44. // onConfirm: () => exit(1),
  45. // );
  46. });
  47. // // 尝试获取一次人员信息
  48. // getCurrent().then((person) {
  49. // user(person);
  50. // }).catchError((e) {
  51. // print(e);
  52. // });
  53. }
  54. }