Some times i need to add new value to my data model, but because i use json as a database, i need to change already exist json files, and add new value in them
error happened when i added new value to CheckList Point class, and then try to display data that was created before i add new value
this is function where i load data from my json files
static Future<void> loadData() async {
final dir = await getApplicationDocumentsDirectory();
List<FileSystemEntity> files = await dir.list().toList();
for (int i = 0; i < files.length; i++) {
String filepath = files[i].path;
File newFile = File(filepath);
String name = p.basenameWithoutExtension(newFile.path);
String myExtension = p.extension(filepath);
if (myExtension != '.cl') {
} else {
String data = await newFile.readAsString();
try {
checkLists.add(CheckListModel.fromJson(jsonDecode(data)));
} catch (e) {
if (e.toString() == "type 'Null' is not a subtype of type 'String' in type cast") {
}
}
}
}
}
I have to do something in try cath block, but i have no idea what i can to
also checkLists is static, i use it in listview.builder on another screen of my app
I use json_serializable package to create from json and to json functions
this is functions that i use to write and read data
Future<File> writeToFile(map) async {
final file = await localFile;
return file.writeAsString(jsonEncode(map));
}
Future<String> get localPath async {
final directory = await getApplicationDocumentsDirectory();
return directory.path;
}
Future<File> get localFile async {
final path = await localPath;
return File('$path/${_guid}.cl');
}
CheckListModel class
class CheckListModel {
static List<CheckListModel> checkLists = [];
String? _guid;
String name = "";
int position = 0;
String buttonText = "Начать проверку";
String description = '';
List<CheckListPoint> checkListPoints = [];
int checkListCount = 0;
int chekListExecution = 0;
int checkListResult = 0;
CheckListModel(this.name, this.position, this.buttonText, this.checkListCount, this.checkListResult, this.chekListExecution, this.description, this.checkListPoints, [var guid]) {
if (guid == null) {
Uuid uuid = Uuid();
_guid = uuid.v1();
} else {
_guid = guid.toString();
}
}
get guid {
return _guid.toString();
}
checkListPoint class
class CheckListPoint {
bool correctly = true;
bool passed = false;
String requirement = '';
String notes = " ";
CheckListPoint(this.correctly, this.passed, this.requirement, this.notes);
factory CheckListPoint.fromJson(Map<String, dynamic> json) => _$CheckListPointFromJson(json);
Map<String, dynamic> toJson() => _$CheckListPointToJson(this);
}