无法使用 Flutter 读取本地目录中的文件
Unable to read a file in local directory with Flutter
我正在创建一个将文件写入本地目录并需要稍后访问它的应用程序。一个问题:当我尝试读取文件时,进程永远不会结束(也不会抛出任何错误)。
这是我从代码中提取的方法。
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;
}
当我执行此代码时,在控制台中“读取数据...”后没有任何反应:
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()
from File
.
求助,我只想读取本地存储的 JSON 文件...
我解决了这个问题。
问题是我的文件不存在,创建文件时路径出错(所以文件的路径在我创建时和我试图用另一种方法访问它时是不同的).
无论如何,我觉得代码这样反应很奇怪。它至少应该打印一些东西,或者告诉我出了什么问题。我不得不考虑解决方法以了解问题所在...
我正在创建一个将文件写入本地目录并需要稍后访问它的应用程序。一个问题:当我尝试读取文件时,进程永远不会结束(也不会抛出任何错误)。
这是我从代码中提取的方法。
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;
}
当我执行此代码时,在控制台中“读取数据...”后没有任何反应:
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
.
求助,我只想读取本地存储的 JSON 文件...
我解决了这个问题。
问题是我的文件不存在,创建文件时路径出错(所以文件的路径在我创建时和我试图用另一种方法访问它时是不同的).
无论如何,我觉得代码这样反应很奇怪。它至少应该打印一些东西,或者告诉我出了什么问题。我不得不考虑解决方法以了解问题所在...