从单词列表中随机选择一个元素时,为什么我在元素末尾得到一个空白字符
Why am I getting an blank character at the end of an element when randomly selecting an element from a word list
我必须对 selectedWord 进行硬编码以避免空白字符
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);
}
}
});
}
我实施了 dataString.replaceAll("\r\n", "\n").split("\n") 并通过删除导致额外项目的 \r 解决了问题在拆分列表中
我必须对 selectedWord 进行硬编码以避免空白字符
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);
}
}
});
}
我实施了 dataString.replaceAll("\r\n", "\n").split("\n") 并通过删除导致额外项目的 \r 解决了问题在拆分列表中