I'm creating an app that writes a file into local directory and needs to access it later. One problem: when I try to read the file, the process never ends (and no error is thrown).
Here is the method I extracted from my code.
import 'dart:convert';
import 'dart:io';
import "package:path_provider/path_provider.dart";
Future<Map<String, dynamic>> getFile(String path) async {
final dir = await getApplicationDocumentsDirectory();
final rootPath = dir.path;
final file = File("$rootPath/$path");
print("getting file at : '$rootPath/$path'");
print("reading data...");
final content = file.readAsStringSync(); // what is happening here??
print("file read!"); // this line is never reached.
print("The content has been decoded as a string : '$content'");
return content;
}
When I execute this code, nothing happens after "reading data..." in the console:
I/flutter (16575): getting file at : '/data/user/0/com.example.app/app_flutter/Folder/subfolder/file.json'
I/flutter (16575): reading data...
D/EGL_emulation(16575): app_time_stats: avg=6.00ms min=3.32ms max=39.17ms count=60
D/EGL_emulation(16575): app_time_stats: avg=4.48ms min=2.67ms max=6.84ms count=57
etc.
... this line above is repeating itself indefinitely and my app is showing the loader
NOTE : The function to write the file works perfectly fine.
NOTE : I get the same problem with the async method
readAsString()
fromFile
.
Please, help, I just want to read a JSON file stored locally...
CodePudding user response:
I fixed the issue.
The issue was that my file didn't exist, an error in the path when creating the file (so the path to a file was different when I created it, and when I was trying to access it in another method).
Anyways, I think this is strange that the code reacts like this. It should print something, or tell me that something went wrong, at least. I had to think about workarounds to see what the issue was...