我正在遍歷串列以獲取具有布林值false的專案。
類:
class Words {
String alphabet;
String word;
String wordtype;
String image;
bool used;
Words({
required this.alphabet,
required this.word,
required this.wordtype,
required this.image,
required this.used,
});
}
以下是正在使用的示例資料
List<Words> initData = [
Words(
alphabet: 'a',
word: 'apple',
wordtype: 'fruit',
image: 'assets/res/apple.jpg',
used: false),
Words(
alphabet: 'a',
word: 'ant',
wordtype: 'insect',
image: 'assets/res/ant.jpg',
used: false),
Words(
alphabet: 'a',
word: 'axe',
wordtype: 'object',
image: 'assets/res/axe.jpg',
used: false),
Words(
alphabet: 'a',
word: 'arrow',
wordtype: 'object',
image: 'assets/res/arrow.jpg',
used: false),
Words(
alphabet: 'a',
word: 'airplane',
wordtype: 'object',
image: 'assets/res/airplane.jpg',
used: false),
Words(
alphabet: 'a',
word: 'anchor',
wordtype: 'object',
image: 'assets/res/anchor.jpg',
used: false),
Words(
alphabet: 'a',
word: 'alligator',
wordtype: 'animal',
image: 'assets/res/alligator.jpg',
used: false),
];
呼叫小部件
class WordsWidget extends StatelessWidget {
final homeCtrl = Get.find<HomeController>();
WordsWidget({Key? key}) : super(key: key);
@override
Widget build(BuildContext context) {
print(homeCtrl.nextWord());
return Container();
}
}
以下函式迭代,如果所有單詞都已使用,則將它們重置并再次迭代。
職能:
nextWord() {
Words nextword =
Words(alphabet: '', word: '', wordtype: '', image: '', used: false);
bool found = false;
for (var i = 0; i < words.length; i ) {
if (words[i].used == false && found != true) {
nextword = words[i];
words[i].used = true;
found = true;
}
}
if (found == false) {
print("resetting");
resetWords();
} else {
print("Found = true so now returning");
print(nextword.word);
return nextword.word;
}
}
void resetWords() {
for (var a = 0; a < words.length; a ) {
words[a].used = false;
}
nextWord();
}
以上作業正常,直到它重置。重置后,即使上面的列印回傳顯示了該值,但是當我在呼叫小部件時列印相同的值時,它回傳 null。以下是說明問題的輸出。
flutter: Found = true so now returning
2 flutter: arrow
flutter: Found = true so now returning
2 flutter: airplane
flutter: Found = true so now returning
2 flutter: anchor
flutter: Found = true so now returning
2 flutter: alligator
flutter: resetting
flutter: Found = true so now returning
flutter: apple
flutter: null
正如你從上面看到的,在 alligator 這個詞之后,它會重置它自己并為所有詞設定 used = false。然后它在迭代中搜索下一個單詞,它找到了單詞 apple,但呼叫該函式的小部件仍然回傳 null。
如果有人能強調我做錯了什么,我將不勝感激。謝謝你。
uj5u.com熱心網友回復:
您當前的代碼有兩個主要問題
words如果您的串列為空,它將陷入無限遞回(因此是例外) 。如果您沒有在
for回圈中找到可用的單詞,則不會回傳任何內容...
nextWord() {
// if there are no available words, don't need to do anything
if (words.length == 0) {
return null;
}
for (int i = 0; i < words.length; i ) {
//when you found an unused word, set the flag and return the word
if (!words[i].used) {
words[i].used = true;
return words[i].word;
}
}
//in case you didn't find anything, return the result of resetWords
return resetWords();
}
resetWords() {
//reset the flags
for (int i = 0; i < words.length; i ) {
words[i].used = false;
}
//and call nextWord again, and return its result
return nextWord();
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/439623.html
