Sqflite是否支持保存自定义对象类型?

时间:2019-06-17 05:55:31

标签: flutter dart sqflite

如何在Sqflite数据库中存储此NetworkList和Map类型?我不知道表列中的确切类型是什么?

 @JsonSerializable()
 class NetworkResponseData {
 @JsonKey(name: "status")
 String status;
 @JsonKey(name: "language")
 Map<String, String> language;
 @JsonKey(name: "networks")
 NetworkList networkList;


 NetworkResponseData(this.status, this.language, this.networkList,
  this.siteUrlMap);


}

@JsonSerializable()
class NetworkList {
@JsonKey(name: "en")
List<Map<String, String>> en;
@JsonKey(name: "fr")
List<Map<String, String>> fr;
@JsonKey(name: "ge")
List<Map<String, String>> ge;
@JsonKey(name: "sp")
List<Map<String, String>> sp;

NetworkList(this.en, this.fr, this.ge, this.sp);

}

1 个答案:

答案 0 :(得分:1)

所以您要寻找在Flutter中序列化对象的方法吗?在Flutter中,您可以通过导入dart:convert并使用jsonEncode / jsonDecode方法来实现;

您可以像这样手动进行操作: 假设您有一个模型。

class DoughnutModel {

 String Name;
 String Filling;
 String Topping;
 Double Price;
 }

创建甜甜圈对象很简单:

DoughnutModel myDoughnut = new DoughnutModel("Glazed", "None", "Sprinkles", 2.99);

现在我们可以使用jsonEncode对其进行序列化:

String encodedDoughnut = jsonEncode(myDoughnut); //you can now store this string in the shared preferences or local storage.

并使用Decoding这样检索对象:

Map<String, dynamic> decodedDoughnut = jsonDecode(encodedDoughnut);

示例和概念均来自此链接: https://medium.com/flutter-community/serializing-your-object-in-flutter-ab510f0b8b47