我试图读取多个文件,以便可以从每个文件中提取信息。当我尝试读取第二个文件时,我得到
:未处理的异常: RangeError(索引):无效值:仅有效值为0:1
我有一个名为file的对象,并且在初始化新文件之前尝试将其设置为null。
此功能来自错误:
Future readFile() async {
print("What text file would you like to read?(Don't forget the .txt)");
var fileName = stdin.readLineSync();
file=null;
file = File(fileName);
}
这是我叫的方法
addExamGrade()async{
await readFile();
await file.readAsLines().then((lines) =>
lines.forEach((l) => addIndividualEx(l))//print(l)
);
}
我从这里的文件中读取信息:
addIndividualEx(String line)async{
var split;
var grade;
String fname;
String lname;
double ExGrade;
if (line != null) {
split=line.split(',');
grade=split[1].split(' ');
lname=split[0];
fname=grade[1];
ExGrade=double.parse(grade[2]);
if(!students.containsKey(fname)){//checks first name)
students[fname]=Student(fname, lname);
}
students[fname].addExamGrade(ExGrade);
}
}
我想读取多个文件。 但是相反,我得到了错误:
Unhandled exception:
RangeError (index): Invalid value: Only valid value is 0: 1
0 List.[] (dart:core-patch/growable_array.dart:147:60)
1 addIndividualHW (file:///C:/Users/Andrea%20Fernanda%20Torr/IdeaProjects/TestDart/bin/main.dart:117:16)
<asynchronous suspension>
2 addHomeworkGrades.<anonymous closure>.<anonymous closure> (file:///C:/Users/Andrea%20Fernanda%20Torr/IdeaProjects/TestDart/bin/main.dart:70:28)
3 List.forEach (dart:core-patch/growable_array.dart:283:8)
4 addHomeworkGrades.<anonymous closure> (file:///C:/Users/Andrea%20Fernanda%20Torr/IdeaProjects/TestDart/bin/main.dart:70:13)
5 _RootZone.runUnary (dart:async/zone.dart:1379:54)
6 _FutureListener.handleValue (dart:async/future_impl.dart:137:18)
7 Future._propagateToListeners.handleValueCallback (dart:async/future_impl.dart:678:45)
8 Future._propagateToListeners (dart:async/future_impl.dart:707:32)
9 Future._completeWithValue (dart:async/future_impl.dart:522:5)
10 Future._asyncComplete.<anonymous closure> (dart:async/future_impl.dart:552:7)
11 _microtaskLoop (dart:async/schedule_microtask.dart:41:21)
12 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50:5)
13 _runPendingImmediateCallback (dart:isolate-patch/isolate_patch.dart:116:13)
14 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:173:5)
答案 0 :(得分:0)
尝试从这里开始:https://dart.dev/articles/libraries/dart-io
您正在创建async
函数,但没有await
进行任何操作!