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. }else{
  17. return location.value!.longitude.toString() + "," + location.value!.latitude.toString();
  18. }
  19. }
  20. @override
  21. void onInit() {
  22. super.onInit();
  23. // 尝试获取 location
  24. requireLocation().then((loc) async {
  25. var _data = await loc.getLocation();
  26. location(_data);
  27. // 监听位置变化
  28. loc.onLocationChanged.listen((LocationData currentLocation) {
  29. location(currentLocation);
  30. });
  31. }).catchError((e) {
  32. print(e);
  33. var message = e.message.toString().contains('CANCELED')
  34. ? '您取消了, 确定后将退出程序'
  35. : e.message;
  36. // Get.dialog();
  37. // Get.defaultDialog(
  38. // title: '提示',
  39. // middleText: message,
  40. // radius: 8,
  41. // textConfirm: '确定',
  42. // onConfirm: () => exit(1),
  43. // );
  44. });
  45. // // 尝试获取一次人员信息
  46. // getCurrent().then((person) {
  47. // user(person);
  48. // }).catchError((e) {
  49. // print(e);
  50. // });
  51. }
  52. }