Flutter 将自定义堆叠对象转换为 json

Posted

技术标签:

【中文标题】Flutter 将自定义堆叠对象转换为 json【英文标题】:Flutter converting custom stacked objects to json 【发布时间】:2021-02-02 12:02:12 【问题描述】:

所以我有一个相对复杂的对象结构,我想将它保存在 SQFLite 中。但是由于这个对象有其他对象的列表,我想我会将子对象的列表转换为 json 并将其保存为文本。

像这样:

    "name": "String",
     "Body": 
           "Object1": [
                        
                        "index": int,
                        
                     ],

           "Object2": [
                       
                       "index":2,
                       
                     ]
             
    

当我按下按钮时,会创建一个新对象并将其添加到数据库中。 (原始插入)

但是出现了这个问题:

Unhandled Exception: DatabaseException(java.lang.String cannot be cast to java.lang.Integer) 

据我了解,这意味着将字符串转换为 int 时出错。

完整的错误代码

E/flutter (20088): [ERROR:flutter/lib/ui/ui_dart_state.cc(177)] Unhandled Exception: DatabaseException(java.lang.String cannot be cast to java.lang.Integer) sql 'INSERT Into workoutPlan (name,id,workoutDays,pastId,timesDone,workoutBody) VALUES (?,?,?,?,?,?)' args [nummero uno, 2, [Monday, Friday], 345, 45, Pause: [timeInMilSec: 900, index: 5], exercise: [goal: 2, weightGoal: [15, 15, 15], name: PushUp, timeGoal: 900, index: 1, repGoal: 3, setGoal: [infinity, 15, 3]]]

我的模型类:

import 'dart:convert';

PlanModel planModelFromJson(String str) => PlanModel.fromJson(json.decode(str));

String planModelToJson(PlanModel data) => json.encode(data.toJson());

class PlanModel 
  PlanModel(
    this.name,
    this.id,
    this.workoutDays,
    this.pastId,
    this.timesDone,
    this.workoutBody,
  );

  String name;
  int id;
  List<String> workoutDays;
  int pastId;
  int timesDone;
  WorkoutBody workoutBody;

  factory PlanModel.fromJson(Map<String, dynamic> json) => PlanModel(
    name: json["name"],
    id: json["id"],
    workoutDays: List<String>.from(json["workoutDays"].map((x) => x)),
    pastId: json["pastId"],
    timesDone: json["timesDone"],
    workoutBody: WorkoutBody.fromJson(json["workoutBody"]),
  );

  Map<String, dynamic> toJson() => 
    "name": name,
    "id": id,
    "workoutDays": List<dynamic>.from(workoutDays.map((x) => x)),
    "pastId": pastId,
    "timesDone": timesDone,
    "workoutBody": workoutBody.toJson(),
  ;


class WorkoutBody 
  WorkoutBody(
    this.exercise,
    this.pause,
  );

  List<Exercise> exercise;
  List<Pause> pause;

  factory WorkoutBody.fromJson(Map<String, dynamic> json) => WorkoutBody(
    exercise: List<Exercise>.from(json["exercise"].map((x) => Exercise.fromJson(x))),
    pause: List<Pause>.from(json["Pause"].map((x) => Pause.fromJson(x))),
  );

  Map<String, dynamic> toJson() => 
    "exercise": List<dynamic>.from(exercise.map((x) => x.toJson())),
    "Pause": List<dynamic>.from(pause.map((x) => x.toJson())),
  ;


class Exercise 
  Exercise(
    this.index,
    this.name,
    this.goal,
    this.repGoal,
    this.weightGoal,
    this.timeGoal,
    this.setGoal,
  );

  int index;
  String name;
  int goal;
  int repGoal;
  List<int> weightGoal;
  int timeGoal;
  List<String> setGoal;

  factory Exercise.fromJson(Map<String, dynamic> json) => Exercise(
    index: json["index"],
    name: json["name"],
    goal: json["goal"],
    repGoal: json["repGoal"],
    weightGoal: List<int>.from(json["weightGoal"].map((x) => x)),
    timeGoal: json["timeGoal"],
    setGoal: List<String>.from(json["setGoal"].map((x) => x)),
  );

  Map<String, dynamic> toJson() => 
    "index": index,
    "name": name,
    "goal": goal,
    "repGoal": repGoal,
    "weightGoal": List<dynamic>.from(weightGoal.map((x) => x)),
    "timeGoal": timeGoal,
    "setGoal": List<dynamic>.from(setGoal.map((x) => x)),
  ;


class Pause 
  Pause(
    this.index,
    this.timeInMilSec,
  );

  int index;
  int timeInMilSec;

  factory Pause.fromJson(Map<String, dynamic> json) => Pause(
    index: json["index"],
    timeInMilSec: json["timeInMilSec"],
  );

  Map<String, dynamic> toJson() => 
    "index": index,
    "timeInMilSec": timeInMilSec,
  ;

【问题讨论】:

我发现标题令人困惑。也许改写它会有助于获得好的答案,并且会让有类似问题的人更容易找到 @Alex.F 也许这样更好 ;) 我不确定如何最好地描述这个问题 【参考方案1】:

我发现了问题:)

在 planModel toJson() 我有

"workoutDays": List<dynamic>.from(workoutDays.map((x) => x)),

并将其更改为jsonDecode(workoutDays) 并再次阅读我使用jsonEncode(src)

【讨论】:

【参考方案2】:

Flutter 可能会将您的变量作为字符串获取。 为了避免任何类型问题,我总是这样做:

int.tryParse(workoutBody.toString());

您可能应该对所有存储的数据执行此操作,尤其是在将 SQL 查询 转换为 dart int 然后 json int 时。

【讨论】:

以上是关于Flutter 将自定义堆叠对象转换为 json的主要内容,如果未能解决你的问题,请参考以下文章

Swift 4 如何将自定义对象数组转换为 JSON

如何将自定义 json 转换为自适应卡片 json 格式

将检索到的json转换为flutter中的自定义对象列表

如何正确地将自定义按钮集转换为响应对象?

如何将自定义属性转换为 Json 字符串

Typescript/Angular 12:将自定义对象转换为参数对象