Home > Back-end >  Why am I getting an blank character at the end of an element when randomly selecting an element from
Why am I getting an blank character at the end of an element when randomly selecting an element from

Time:04-05

I have to hard code my selectedWord to avoid the blank character

Future<String> getData() async {
    try {
      return await rootBundle.loadString('text_file/four_words.txt');
    } catch (e) {
      throw (e.toString());
    }
  }
Future<String> val = getData();
val.then((value) {
  setState(() {
    dataString = value;
    var elements = dataString.split("\n");
    elements.forEach((element) {
      wordList.add(element);
      //debugPrint(element);
    });


_selectNewWord() {
    setState(() {
      selectedWord = wordList[_random.nextInt(wordList.length)];
      selectedWord=selectedWord[0] selectedWord[1] selectedWord[2] selectedWord[3];
      var elements2 = selectedWord.split("");
      myList.clear();
      for (var element in elements2) {
        if (element.isNotEmpty) {
          myList.add(element);
        }
      }
    });
  }

CodePudding user response:

I implemented dataString.replaceAll("\r\n", "\n").split("\n") and it solved the problem by removing the \r that was causing the extra item in the split list

  • Related