So I have a relatively complex object structure and I want to save it in SQFLite. But since this object has list of other objects, I thought that I would convert the list of subobjects to json and save this as text.
Like this:
"name": "String",
"Body": {
"Object1": [
{
"index": int,
}
],
"Object2": [
{
"index":2,
}
]
}
When I press a button a new object is created and added to the database. (rawInsert)
But this problem arises:
Unhandled Exception: DatabaseException(java.lang.String cannot be cast to java.lang.Integer)
Which as far as I understand it means that there was an error converting a string to an 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,
};
}
I found the problem :)
In the planModel toJson() I had
"workoutDays": List<dynamic>.from(workoutDays.map((x) => x)),
and changed it to jsonDecode(workoutDays)
and to read it again I used jsonEncode(src)