I'm moving from Android Studio to flutter. In the Android Studio project , I write files in a JSON format using this code:
String json = gson.toJson(item);
FileOutputStream fos = null;
try {
fos = context.openFileOutput("item"+ item.getUid(), Context.MODE_PRIVATE);
ObjectOutputStream os = new ObjectOutputStream(fos);
os.writeObject(json);
os.close();
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I now want to load in these files in Flutter SDK using this code
Future<List<Item>> loadAllItems() async {
try{
var items = <Item>[];
var filesDir = Directory("/data/user/0/com.company.app/files");
for(var f in filesDir.listSync()){
if(f is File){
f.readAsString(encoding: latin1).then((jsonstr) => {
items.add(parse(jsonstr))
});
}
}
return items;
} catch(e){
log(e);
}
return [];
}
But the problem is that the string jsonstr
is now prefixed by weird characters.
Here's how it is saved and loaded in at Android Studio
{"property1":"value1", ...}
and this is how it's read in in Flutter SDK
’ t${"property1":"value1", ... }
I also tried using utf8 encoding but this raises an exception
Failed to decode data using encoding 'utf-8'
So how to get the normal JSON string without the weird symbols before it?