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

Flutter converting custom stacked objects to json

所以我有一个比较复杂的对象结构,我想把它保存在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]}]}]}

我的模型class:

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,
  };
}

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

int.tryParse(workoutBody.toString());

您可能应该对所有存储的数据执行此操作,尤其是在将 SQL query 转换为 dart int[=18= 时] 然后 json int.

我发现了问题:)

在 planModel toJson() 我有

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

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