变量在 Flutter 中意外返回 null

Variable unexpectedly returning null in Flutter

我正在遍历列表以获取具有 bool 值 false.

的项目

Class:

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),
  ];

正在调用 Widget

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();
  }

以上在重置之前工作正常。重置后,即使上面的打印 return 显示了该值,但是当我在调用小部件时打印相同的值时,它 return 为空。以下是说明问题的输出。

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 但调用该函数的小部件仍然 returns null.

如果有人能指出我做错了什么,我将不胜感激。谢谢。

您当前的代码存在两个主要问题

  1. 如果您的 words 列表为空,它将 运行 进入无限递归(因此是异常)。

  2. 以防万一,您在 for 循环中找不到可用的词,您 return 什么都没有...

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();
}