Reading a file in dart and split the string has different results in console that in vscode

Viewed 192

I'm new in dart, I'm trying to read information from a txt file and use the data to create objects from a class (in this case about pokemon), but when I run my program in the terminal it doesn't prints the correct information, and when I run the program in vscode (whit the dart extension, the "run" button) it prints in the debug console the correct information. What is the problem?

When I run the program in vscode I get in my print method (printP) this (which is what I want)

vscode:

Print method:
1+: Bulbasaur GRASS | POISON

but when I run the program in the terminal I get this.

Terminal:

Print method:
 | POISONsaur

Here is the dart code.

main.dart

import 'dart:io';
import 'pokemon.dart';

void main() {
  var file = new File("/home/ariel/Documents/script/pokemon.txt");
  String str = file.readAsStringSync();
  var pokes = str.split("[");
  pokes = pokes.sublist(1, pokes.length);
  getPokemon(pokes[0]).printP();
}

Pokemon getPokemon(String str) {
  Pokemon p = new Pokemon();
  print("string: " + str);
  var aux = str.split("\n");
  print(aux.length);
  for (var i in aux) {
    print("line: " + i);
  }
  p.number = int.parse(aux[0].split("]")[0]);
  p.name = aux[1].split("=")[1];
  p.type1 = aux[3].split("=")[1];
  p.type2 = aux[4].split("=")[1];
  return p;
}

pokemon.dart

class Pokemon {
  String _name, _type1, _type2;
  int _number;

  Pokemon() {
    this._name = "";
    this._number = 0;
    this._type1 = "";
    this._type2 = "";
  }

  void printP() {
    print("Print method:");
    print("${this._number}+: ${this._name} ${this._type1} | ${this._type2}");
  }

  void set number(int n) {
    this._number = n;
  }

  void set name(String nm) {
    this._name = nm;
  }

  void set type1(String t) {
    this._type1 = t;
  }

  void set type2(String t) {
    this._type2 = t;
  }
}


And here is the txt file

pokemon.txt

[1]
Name=Bulbasaur
InternalName=BULBASAUR
Type1=GRASS
Type2=POISON
BaseStats=45,49,49,45,65,65
GenderRate=FemaleOneEighth
GrowthRate=Parabolic
BaseEXP=64
EffortPoints=0,0,0,0,1,0
Rareness=45
Happiness=70
Abilities=OVERGROW
HiddenAbility=CHLOROPHYLL
Moves=1,TACKLE,3,GROWL,7,LEECHSEED,9,VINEWHIP,13,POISONPOWDER,13,SLEEPPOWDER,15,TAKEDOWN,19,RAZORLEAF,21,SWEETSCENT,25,GROWTH,27,DOUBLEEDGE,31,WORRYSEED,33,SYNTHESIS,37,SEEDBOMB
EggMoves=AMNESIA,CHARM,CURSE,ENDURE,GIGADRAIN,GRASSWHISTLE,INGRAIN,LEAFSTORM,MAGICALLEAF,NATUREPOWER,PETALDANCE,POWERWHIP,SKULLBASH,SLUDGE
Compatibility=Monster,Grass
StepsToHatch=5355
Height=0.7
Weight=6.9
Color=Green
Habitat=Grassland
Kind=Seed
Pokedex=Almacena energía en el bulbo de su espalda para alimentarse durante épocas de escasez de recursos o para atacar liberándola de golpe.
BattlerPlayerY=0
BattlerEnemyY=25
BattlerAltitude=0
Evolutions=IVYSAUR,Level,16

1 Answers

Your code are dependent on the newline format of your txt file. I will recommend you are using the LineSplitter class from dart:convert to split your lines.

The problem is that Windows newlines contains both '\n' and '\r' but you are only removing the '\n' part. '\r' are essential meaning the terminal should set the cursor back to the beginning of the line.

You can read this like a typewriter where you first move the head back and set move the paper to the next line. And can read a lot more about is topic here: https://en.wikipedia.org/wiki/Newline

The purpose of the LineSplitter class is to abstract all of this logic and get some behavior which will work on all platforms.

So import dart:convert and change this line:

var aux = str.split("\n");

Into:

var aux = LineSplitter.split(str).toList();
Related