【问题标题】:Save data as objects using shared preferences in flutter在颤振中使用共享首选项将数据保存为对象
【发布时间】:2020-08-26 10:14:53
【问题描述】:

当我使用 Flutter 改进自己时,我有一个 User 模型,其中包含 pet Dog Pet 模型。每个模型都有一个条目的 ID。我需要知道如何使用 Flutter 中的共享首选项将这些数据保存为具有 ID 值的对象。我从互联网上关注了几个例子,但还没有抓住解决方案。以下是我目前尝试过的。

用户模型

int id;
  String userName;
  String userEmail;
  String userPassword;
  String userAddress;
  String userPhoneNo;
  String userCountry;
  bool isSubscribed = false;
  String role;


  User({this.userName, this.userEmail, this.userPassword, this.userAddress,
      this.userPhoneNo, this.userCountry, this.isSubscribed, this.role});

  factory User.fromJson(Map<String, dynamic> parsedJson) {
    return new User(
        userName: parsedJson['userName'] ?? "",
        userEmail: parsedJson['userEmail'] ?? "",
      userPassword: parsedJson['userPassword'] ?? "",
      userAddress: parsedJson['userAddress'] ?? "",
      userPhoneNo: parsedJson['userPhoneNo'] ?? "",
      userCountry: parsedJson['userCountry'] ?? "",
      isSubscribed: parsedJson['isSubscribed'] ?? false,
      role: parsedJson['role'] ?? "",
    );
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> data = new Map<String, dynamic>();
    data['userName'] = this.userName;
    data['userEmail'] = this.userEmail;
    data['userAddress'] = this.userAddress;
    data['userPhoneNo'] = this.userPhoneNo;
    data['userCountry'] = this.userCountry;
    data['isSubscribed'] = this.isSubscribed;
    return data;
  }
}

宠物模型

in id;
  String petImage;
  String petName;
  int petAge;
  String petBreed;
  double petWeight;
  double petIdealWeight;
  String petSex;
  String petEatBones;
  String petBirthDate;

  Pet(this.petImage, this.petName, this.petAge, this.petBreed, this.petWeight,
      this.petIdealWeight, this.petSex, this.petEatBones, this.petBirthDate);
}

我需要在哪里使用共享首选项保存和查看数据

var userData = {
      'username': _userName,
      'email': _userEmail,
      'password': _userPassword,
      'address': _userAddress,
      'country': _selectedCountry.toString(),
      'mobile': mobile,
      'subscribed': _isSubscribed,
      'role': _role,
    };

//save user data in userPrefs
      userPrefs = await SharedPreferences.getInstance();
      // var userJson = userPrefs.getString('user');
      Map decode_options = jsonDecode(jsonString);
      String user = jsonEncode(User.fromJson(decode_options));
      userPrefs.setString('userData', user);

//get those data
@override
  void initState() {
    _getUserInfo();
    super.initState();
  }

  void _getUserInfo() async {
    userViewPrefs = await SharedPreferences.getInstance();
    // get  user info from prefs //
    Map jsonString = jsonDecode(userViewPrefs.getString('userData'));
    var user = User.fromJson(jsonString);

    setState(() {
      userData = user;//userStringDecode;
    });

【问题讨论】:

    标签: flutter sharedpreferences


    【解决方案1】:

    你可以试试这个。

    设置数据

      userPrefs = await SharedPreferences.getInstance();
      String user = jsonEncode(User.toJson(your json));  //json data
      userPrefs.setString('userData', user);
    

    获取数据

    userViewPrefs = await SharedPreferences.getInstance();
    
     String user= userViewPrefs.getString('userData')
    Map jsonString = json.decode(user);
    User user = User.fromJson(jsonString);
    

    【讨论】:

    • 嗨,Jhonny,你能解释一下我从哪里得到这个“你的 json”吗?这是我不明白的要点。
    猜你喜欢
    • 1970-01-01
    • 2019-05-24
    • 2020-04-02
    • 2021-08-16
    • 2021-12-29
    • 2017-12-02
    • 2013-10-18
    • 2011-08-23
    • 1970-01-01
    相关资源
    最近更新 更多