简体   繁体   English

从 flutter 中的资产中的文件读取后,逐行读取字符串

[英]Read String line by line after read from file in assets in flutter

I can read the file successfully, but only one part of the lines was processed.我可以成功读取文件,但只处理了一部分行。 As you can see: split the raw string by the method split of String class, I do not know why.如您所见:通过String类的split方法分割原始字符串,不知道为什么。 Is there another way to split String into lines?还有另一种方法可以将 String 拆分成行吗?

String rawStr = rootBundle.loadString('assets/sql/create_tables');
List<String> list = rawStr.split("\n");
list.map( (e) {
    print("> $e");
    db.execute(e);
});

I expect all the lines will be processed.我希望所有的行都会被处理。

The code read from assets and process line by line should be as follows.从资产和流程中逐行读取的代码应如下所示。

    String rawStr = rootBundle.loadString('assets/sql/create_tables');
Iterable<String> list = LineSplitter.split(rawStr);
list.forEach((e){
if (e?.isEmpty ?? true) {
    //Do nothing
} else {
    print("> $e");
    db.execute(e);
}
});

try using forEach() as seen below.尝试使用forEach() ,如下所示。

list.forEach((e){
   print("> $e");
   db.execute(e);
});

Good Luck.祝你好运。

Here is how you can read the strings using LineSplitter:以下是使用 LineSplitter 读取字符串的方法:

  void _loadFile() async {
    String data = await rootBundle.loadString('assets/myfile.txt');
    LineSplitter.split(data).forEach((line) => print('$line'));
  }

Replace print with your data loading function.用您的数据加载功能替换打印。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM