Flutter:如何从简单的电子表格中读取数据? [英] Flutter: How to read data from simple spreadsheet?
问题描述
我是扑朔迷离的新手,并且有一个不难为专业人士回答的问题.
I am new to flutter and a have question that shouldn't be to hard to answer for a pro.
我有一个简单的电子表格,其中包含5列10行.现在,我有两个变量,分别代表列索引和行索引.
I have a simple spreadsheet with 5 columns and 10 rows. Now I have two variables, representing column and row index.
我想简单地从电子表格中读取相应的值,具体取决于所需的列和行号.
I want to simply read the corresponding value out of the spreadsheet, depending on the wanted column and row numbers.
这可能是扑扑吗? Flutter可以阅读电子表格(例如.csv)并以某种方式获取信息吗?
Is this possible with flutter? Can flutter read a spreadsheet (.csv e.g.) and somehow get the information out of it?
我期待着一个答案,谢谢!
I'm looking forward to an answer, thank you!
这是我到目前为止的代码,最初来自 https://flutter.io/cookbook/persistence/reading-writing-files/. 它会打印
This is the code I have so far, originally from https://flutter.io/cookbook/persistence/reading-writing-files/. It prints
I/flutter(18817):未来"的实例
I/flutter (18817): Instance of 'Future'
但是我不知道如何访问数据.
but I don't know how to access the data.
Future<String> readTable() async {
try {
final file = File("assets/res/table.txt");
// Read the file
String contents = await file.readAsString();
print(contents);
return contents;
} catch (e) {
// If we encounter an error, return 0
return "";
}
}
推荐答案
感谢您的回答.我使用async
和await
使其工作,如下所示.同样重要的是像
Thanks for your answers. I made it work using async
and await
as can be seen in the following. Important as well is to declare the asset file in your pubspec.yaml
like
flutter:
assets:
- assets/res/Book1.csv
然后声明这两个函数,并在要加载数据时调用loadCSV()
.
Then declare both functions and just call loadCSV()
when you want to load the data.
Future<String> loadAsset(String path) async {
return await rootBundle.loadString(path);
}
void loadCSV() {
loadAsset('assets/res/Book1.csv').then((dynamic output) {
csvRaw = output;
});
}
希望这可以帮助遇到相同问题的任何人.
Hope this helps anyone having the same problem.
这篇关于Flutter:如何从简单的电子表格中读取数据?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!