从txt文件中获取内容,然后将其放入列表中
这是我在 stackoverflow 上的第一个问题。在讨论这个问题之前,我想指出,我是 Flutter 和 Dart 的初学者,之前没有移动开发经验。
现在,正如标题所示,我在尝试提取文本文件的内容并将各个名称存储在列表的索引中时遇到了一些问题。我的应用程序的最终目标是打印这些名称的永无止境的列表,每个名称之间用逗号分隔。例如:
top model,
torcitóre,
torcolière,
tornitóre,
tosacàni,
tossicòlogo,
tour operator,
tracciatóre,
tranvière,
trattorìsta,
trattóre²,
trebbiatóre,
trecciaiòlo,
trevière,
tributarìsta,
trinciatóre,
trivellatóre,
truccatóre,
tubìsta,
turnìsta,
名为 jobs.txt
的文本文件包含 1000 多个意大利职位名称。经过几个小时的搜索,我所拥有的就是这个(从另一个用户那里获得):
import 'dart:async';
import 'dart:convert';
import 'dart:io';
void main(List<String> arguments) {
try {
final _jobs = File('jobs.txt');
List<String> lines = _jobs.readAsLinesSync(encoding: ascii);
for (var l in lines) print(l);
} catch (Exception) {
print(Exception.toString());
}
}
jobs.txt
与 dart 文件位于同一目录中,即 bin
。
在尝试为 _jobs
变量提供相对路径时:
FileSystemException: Cannot open file, path = 'binjobs.txt' (OS Error: Impossibile trovare il file specificato.
, errno = 2)
使用绝对路径:
FileSystemException: Cannot open file, path = 'C:UsersUserDesktopdartdart_application_injobs.txt' (OS Error: La sintassi del nome del file, della directory o del volume non è corretta.
, errno = 123)
出于我不知道的原因,如果这很重要,在错误消息中指定的路径中, \ 看起来就像它们不一样甚至不存在。
顺便说一句,这是我的项目结构: 项目结构
如果您的解决方案包含使用 Future 类或 async-await 关键字,那么我'如果您能向我解释它们是如何工作的,我将不胜感激。
更新:查看评论以获取此问题的完整解决方案。经过验证的答案并不是完整的解决方案。
this is my first question here on stackoverflow. Before i get into the question itself, i want to point out that i'm a beginner on Flutter and Dart and have no previous experience in mobile development.
Now, as the title says i'm having some issues while trying to extract the content of a text file and store the individual names inside the indexes of a list. The end goal of my application is to print a never ending list of these names, each separated to another by a comma. For example:
top model,
torcitóre,
torcolière,
tornitóre,
tosacàni,
tossicòlogo,
tour operator,
tracciatóre,
tranvière,
trattorìsta,
trattóre²,
trebbiatóre,
trecciaiòlo,
trevière,
tributarìsta,
trinciatóre,
trivellatóre,
truccatóre,
tubìsta,
turnìsta,
The text file, named jobs.txt
contains more than a 1000+ italian job names. After several hours searching, all i have here is this (got from another user):
import 'dart:async';
import 'dart:convert';
import 'dart:io';
void main(List<String> arguments) {
try {
final _jobs = File('jobs.txt');
List<String> lines = _jobs.readAsLinesSync(encoding: ascii);
for (var l in lines) print(l);
} catch (Exception) {
print(Exception.toString());
}
}
jobs.txt
is inside the same directory as the dart file, which is bin
.
While trying to give the _jobs
variable the relative path:
FileSystemException: Cannot open file, path = 'binjobs.txt' (OS Error: Impossibile trovare il file specificato.
, errno = 2)
With absolute path:
FileSystemException: Cannot open file, path = 'C:UsersUserDesktopdartdart_application_injobs.txt' (OS Error: La sintassi del nome del file, della directory o del volume non è corretta.
, errno = 123)
For reasons i don't know, if that even matters, in the path specified in the error message, the \ look like they don't even exist.
By the way, this is my project structure:
project structure
If your solution includes the usage of the Future class or async-await keywords, i'll be thankful if you could explain me how they work.
UPDATE: Look in the comments for the full solution to this issue. The verified answer isn't the full solution.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
,在您的PubSpec.yaml中创建一个资产文件夹
。
在您的项目中 您可以按照逗号将其拆分,并根据需要在所有工作中循环
Create an assets folder in your project, in your pubspec.yaml specify the asset (make sure it's in your assets folder, that the file exists and is readable)
Now you can access your text file like so -
When the function returns your text, you can split it by commas and loop through all the jobs as you wish
尝试使用rootbundle
try using rootBundle