0

So I am working with Flutter but each time I Hot Restart the app it overwrites my data.json.

// reference one of the data.json
regulatorAsync(licenseText) async {
  Directory dir = await getApplicationDocumentsDirectory();
  File file = File('${dir.path}/data1.json');
  if (!await file.exists()) {
    print("File doesn't exist");
    // if it doesn't exist, create it
    file = await file.create();
    file = await file.writeAsString(await file.readAsString());
  }
  if (await file.readAsString() == "") {
    print("File is empty");
    file = await file.writeAsString('{"newuser": true}');
  }
  var json = jsonDecode(await file.readAsString());
  print(json);
  var a = regulator(json, licenseText);
  return a;
}
//reference 2
onPressed: () async {
          // write data to file
          var data = await rootBundle.loadString('lib/mainapp/data.json');
          var js = jsonDecode(data);
          js["newuser"] = false;
          var js2 = jsonEncode(js);
          // get the path to the document directory.
          Directory tempDir = await getTemporaryDirectory();
          var appDocPath = tempDir.path;
          print(js2);
          var file = await File('$appDocPath/data1.json').writeAsString(js2);
          print(file.readAsStringSync());
          Navigator.of(context).pushReplacementNamed('/AllowPerms');
        },

I know for sure there are no other refrences I even changed the names of the files to data1.json. I get back {"newusers": true} while with the onPress it should have been set to false.

koen1711
  • 3
  • 4

0 Answers0