我正在开发一个原型(prototype)应用程序,它可以从大型 JSON 文件存储库中读取和显示数据。我目前想知道在 pubspec.yaml 和 flutter 代码本身中对这种行为进行建模的最佳方法。
现在我最初加载一个 JSON 格式的目录文件,这会创建一个可滚动的目录条目 ListView ,每个条目在子目录(相对于目录文件)中都有自己的 JSON 文件。我需要将每个新文件添加到 pubspec.yaml 中,还是可以直接访问它们。
最佳答案
您也可以只访问文件系统中的文件,而无需在 pubspec.yaml
中指定文件。
我不确定语法是否仍然正确,因为这是我从事的一个旧项目,但它应该以类似的方式工作。
File _themeFile;
Future<Null> _loadThemeFile() async {
String dir = (await PathProvider.getApplicationDocumentsDirectory()).path;
_themeFile = new File('$dir/theme.txt');
if (!await _themeFile.exists()) {
_themeFile.create();
await _themeFile.writeAsString(JSON.encode({
'theme': 'dark',
'colorIndex': 0,
'displayDone': true,
}));
}
}
此代码段查找名为 theme.txt
的文件并返回它。如果不存在,则创建该文件。您可能不会完全需要它,但我认为您可以将其用作起点。
关于android - 使用 Flutter 访问大量 JSON Assets ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47188990/