简体   繁体   中英

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. Is there another way to split String into lines?

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.

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

Good Luck.

Here is how you can read the strings using 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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