使用flutter中的共享首选项将数据另存为对象

时间:2020-08-26 10:14:53

标签: flutter sharedpreferences

随着我用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;
    });

1 个答案:

答案 0 :(得分:0)

您可以尝试一下。

设置数据

  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);
相关问题