'Dart FormatException: Unexpected character (at character 1) in empty json

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

}


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source