UserProvider.dart 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. import 'dart:convert';
  2. import 'dart:typed_data';
  3. import 'package:flutter/material.dart';
  4. import 'package:flutter_habit/common/LocalData.dart';
  5. import 'package:flutter_habit/pages/home_page.dart';
  6. class UserProvider extends ChangeNotifier {
  7. String? token;
  8. int? uid;
  9. String? email;
  10. String? userName;
  11. String? gender;
  12. String? birthday;
  13. Uint8List? photo;
  14. int? coins;
  15. void init() {
  16. load();
  17. debugPrint("""init AccountProvider to:
  18. token = $token
  19. uid = $uid
  20. email = $email
  21. userName = $userName
  22. gender = $gender
  23. birthday = $birthday
  24. photo = ${photo != null ? "notNull" : "null"}
  25. coins = $coins""");
  26. }
  27. void store() {
  28. LocalData.getInstance()!.setString("token", token!);
  29. LocalData.getInstance()!.setInt("uid", uid!);
  30. LocalData.getInstance()!.setString("email", email!);
  31. LocalData.getInstance()!.setString("userName", userName!);
  32. LocalData.getInstance()!.setString("gender", gender!);
  33. LocalData.getInstance()!.setString("birthday", birthday!);
  34. LocalData.getInstance()!.setString(
  35. "photo", photo == null ? null : Base64Encoder().convert(photo!));
  36. LocalData.getInstance()!.setInt("coins", coins!);
  37. }
  38. void load() {
  39. token = LocalData.getInstance()!.getString("token");
  40. uid = LocalData.getInstance()!.getInt("uid");
  41. email = LocalData.getInstance()!.getString("email");
  42. userName = LocalData.getInstance()!.getString("userName");
  43. gender = LocalData.getInstance()!.getString("gender");
  44. birthday = LocalData.getInstance()!.getString("birthday");
  45. String? listString = LocalData.getInstance()!.getString("photo");
  46. if (listString == null) {
  47. photo = null;
  48. } else {
  49. photo = Base64Decoder().convert(listString);
  50. }
  51. coins = LocalData.getInstance()!.getInt("coins");
  52. }
  53. Future<void> cleanDataAndBackToHome(BuildContext context) async {
  54. token = null;
  55. uid = null;
  56. // email = null;
  57. userName = null;
  58. gender = null;
  59. birthday = null;
  60. photo = null;
  61. coins = null;
  62. store();
  63. await Navigator.of(context).pushAndRemoveUntil(
  64. MaterialPageRoute(builder: (context) => HomePage()),
  65. (route) => route == null,
  66. );
  67. refresh();
  68. }
  69. void refresh() {
  70. store();
  71. notifyListeners();
  72. }
  73. }