profile.dart 955 B

123456789101112131415161718192021222324252627282930313233343536
  1. import 'package:flutter_secure_storage/flutter_secure_storage.dart';
  2. import 'package:json_annotation/json_annotation.dart';
  3. import 'package:e2ee_chat/common/global.dart';
  4. part 'Profile.g.dart';
  5. @JsonSerializable()
  6. class Profile {
  7. Profile();
  8. factory Profile.fromJson(Map<String, dynamic> json) => _$ProfileFromJson(json);
  9. Map<String, dynamic> toJson() => _$ProfileToJson(json);
  10. int theme = Global.themes[0].value;
  11. String? username;
  12. bool isLogin = false;
  13. String locale = "zh";
  14. String? get token {
  15. if (username == null) return null;
  16. final storage = FlutterSecureStorage();
  17. String? token;
  18. storage.read(key: _tokenKey).then((v) => token = v);
  19. return token;
  20. }
  21. set token(String? token) {
  22. assert(username != null, "username == null when setting token");
  23. final storage = FlutterSecureStorage();
  24. storage.write(key: _tokenKey, value: token);
  25. }
  26. String get _tokenKey => "e2ee_chat token of $username";
  27. }