【问题标题】:where to store current user data locally in Flutter?Flutter 在哪里本地存储当前用户数据?
【发布时间】:2021-08-17 17:36:23
【问题描述】:

对不起,我是新手。据我所知,有两个选项可以在本地保存/缓存数据,使用 Shared Preference 或使用 Hive。

比如说,我有这样的用户模型

class User {
  final String uid;
  final String fullname;
  final String email;
  final String domicile;
  final String profilePicturePath;
  final bool verified;
  final bool banned;
  List<String> attendedEventIDs;
  List<String> followingUserIDs;
  final int numberOfFollowers;
  final int numberOfApprovedEvents;
  final String description;
  final List<Event> upcomingEvents;
}

正在使用我的应用的当前用户将从服务器获取他的数据,然后我将在我的应用中的很多地方使用当前用户实例,所以我认为我应该将其保存在本地。

但是如果我将它保存到共享首选项,在我看来,上面的模型有点复杂,要保存在共享首选项中,即使我可以先将它序列化为字符串(我觉得使用它有点 hacky方法)。但如果我使用 Hive/Sqlite,我觉得只将一条记录保存到 Hive 有点矫枉过正

Flutter 常用的当前用户数据在哪里存储?或者有其他方法吗?

【问题讨论】:

    标签: flutter


    【解决方案1】:

    如果您只在设备上存储这一项,我会选择共享首选项。为您的用户类fromMaptoMapfromJsontoJson 提供辅助方法很容易。当您想存储在共享首选项中时,您调用toJson 将其转换为字符串。当您想将其转换回用户类时,您只需调用 fromJson,它会从 Shared Preferences 中获取字符串并返回一个 User 实例。

    当然,您可以使用 Hive 并为您完成这项工作,但 IMO 这样做有点矫枉过正。

    用户类别:

    import 'dart:convert';
    
    class User {
      final String uid;
      final String fullname;
      final String email;
      final String domicile;
      final String profilePicturePath;
      final bool verified;
      final bool banned;
      List<String> attendedEventIDs;
      List<String> followingUserIDs;
      final int numberOfFollowers;
      final int numberOfApprovedEvents;
      final String description;
      final List<Event> upcomingEvents;
    
      User({
        this.uid,
        this.fullname,
        this.email,
        this.domicile,
        this.profilePicturePath,
        this.verified,
        this.banned,
        this.attendedEventIDs,
        this.followingUserIDs,
        this.numberOfFollowers,
        this.numberOfApprovedEvents,
        this.description,
        this.upcomingEvents,
      });
    
      Map<String, dynamic> toMap() {
        return {
          'uid': uid,
          'fullname': fullname,
          'email': email,
          'domicile': domicile,
          'profilePicturePath': profilePicturePath,
          'verified': verified,
          'banned': banned,
          'attendedEventIDs': attendedEventIDs,
          'followingUserIDs': followingUserIDs,
          'numberOfFollowers': numberOfFollowers,
          'numberOfApprovedEvents': numberOfApprovedEvents,
          'description': description,
          'upcomingEvents': upcomingEvents?.map((x) => x.toMap())?.toList(),
        };
      }
    
      factory User.fromMap(Map<String, dynamic> map) {
        return User(
          uid: map['uid'],
          fullname: map['fullname'],
          email: map['email'],
          domicile: map['domicile'],
          profilePicturePath: map['profilePicturePath'],
          verified: map['verified'],
          banned: map['banned'],
          attendedEventIDs: List<String>.from(map['attendedEventIDs']),
          followingUserIDs: List<String>.from(map['followingUserIDs']),
          numberOfFollowers: map['numberOfFollowers'],
          numberOfApprovedEvents: map['numberOfApprovedEvents'],
          description: map['description'],
          upcomingEvents: List<Event>.from(map['upcomingEvents']?.map((x) => Event.fromMap(x))),
        );
      }
    
      String toJson() => json.encode(toMap());
    
      factory User.fromJson(String source) => User.fromMap(json.decode(source));
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-12-01
      相关资源
      最近更新 更多