app.dart 1.1KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. @override
  14. void onInit() {
  15. super.onInit();
  16. // 尝试获取 location
  17. requireLocation().then((loc) async {
  18. var _data = await loc.getLocation();
  19. location(_data);
  20. // 监听位置变化
  21. loc.onLocationChanged.listen((LocationData currentLocation) {
  22. location(currentLocation);
  23. });
  24. }).catchError((e) {
  25. print(e);
  26. Get.defaultDialog(
  27. title: e.message,
  28. onConfirm: () => exit(1),
  29. );
  30. });
  31. // 尝试获取一次人员信息
  32. getCurrent().then((person) {
  33. user(person);
  34. }).catchError((e) {
  35. print(e);
  36. });
  37. }
  38. }