Basically what the title says, simply trying to implement a condition that either writes to a an existing file or decodes the json and then writes to the same file. code:
import 'dart:io';
import 'dart:convert';
import 'ToDo.dart';
void main() async{
print("hello");
int prio1;
print("Title:");
String? title1 = stdin.readLineSync();
print("description:");
String? des = stdin.readLineSync();
ToDo test = new ToDo(title1, des, 0, false);
String test11 = jsonEncode(test);
print(test11);
final filename = 'vault.json';
if(File(filename).length == 0)
{
new File(filename).writeAsString("[" test11 "]");
}
else{
String test = await jsonDecode(filename);
String test2 = test.substring(0, test.length - 1);
String test3 = "," test2 "]";
new File(filename).writeAsString(test3);
}
}
my json file "vault.json" is empty, yet I get this error:
Unhandled exception:
FormatException: Unexpected character (at character 1)
vault.json
^
#0 _ChunkedJsonParser.fail (dart:convert-patch/convert_patch.dart:1405:5)
#1 _ChunkedJsonParser.parseNumber (dart:convert-patch/convert_patch.dart:1272:9)
#2 _ChunkedJsonParser.parse (dart:convert-patch/convert_patch.dart:937:22)
#3 _parseJson (dart:convert-patch/convert_patch.dart:40:10)
#4 JsonDecoder.convert (dart:convert/json.dart:612:36)
#5 JsonCodec.decode (dart:convert/json.dart:216:41)
#6 jsonDecode (dart:convert/json.dart:155:10)
#7 main (file:///D:/Code/dart/json/json.dart:23:27)
#8 _delayEntrypointInvocation.<anonymous closure> (dart:isolate-patch/isolate_patch.dart:297:19)
#9 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:192:12)
any help appreciated!
edit:added ToDo.dart in case you want to run the program
class ToDo {
String? title,description;
int prio;
bool completed;
ToDo(this.title, this.description, this.prio, this.completed);
ToDo.fromJson(Map<String,dynamic> json):
title = json['title'],
description = json['description'],
prio = json['prio'],
completed = json['completed'];
Map<String,dynamic> toJson() => {
'title':title,
'description':description,
'prio':prio,
'completed':completed,
};
}
CodePudding user response:
I am not exactly sure what you want to achieve but this snippets lets you read and write into that json file.
Please notice that we have to read the json from the file first with readAsLinesSync
before decoding. This caused your error, since you didn't try to decode the json object (for example [{"title":"test","description":"test","prio":0,"completed":false}]
but just the file name (vault.json
), which of course isn't a valid json.
import 'dart:io';
import 'dart:convert';
import 'ToDo.dart';
void main() async{
const filename = 'vault.json';
print('Enter title');
String? title = stdin.readLineSync();
print('Enter description');
String? description = stdin.readLineSync();
final file = File(filename);
final fileIsEmpty = await file.length() == 0;
if(fileIsEmpty)
{
// create a test ToDO
ToDo testToDo = ToDo(title, description, 0, false);
String test11 = jsonEncode(testToDo);
// write to File
await File(filename).writeAsString(test11);
}
else {
// read the ToDO from the file
final json = File(filename).readAsLinesSync();
final encode = jsonDecode(json.first);
ToDo storedToDo = ToDo.fromJson(encode);
print(storedToDo);
}
}
Also consider using snake case for your file names, which is best practice.
Happy coding!