[英]How to assign the return of the readAsLines() to a local List in Flutter?
我剛剛寫了這段代碼:
Future<List<String>> readMedsNames() async{
List<String> medsNames;
final directory = await getApplicationDocumentsDirectory();
File eventFilePointer = File('${directory.path}/events.txt');
if(eventFilePointer.existsSync()){
eventFilePointer.readAsLines().then((lines) =>
print(lines) //Works
//medsNames = lines
); //Doesn't work, when I print it in the flollowing line the value is null...
//print(medsNames);
return medsNames;
}
else
{
return null;
}
}
我必須返回List<String>
填充了我讀過的文件中寫入的所有行。 我能怎么做? 提前致謝!
原因是return
語句命中之前, async
回調完成。 嘗試如下:
if(eventFilePointer.existsSync()) {
medsNames = await eventFilePointer.readAsLines();
print(medsNames);
return medsNames;
}
return null;
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.