在Flutter Birdle教程中更新状态失败
我一直在按照Flutter的教程学习,地址是 https://docs.flutter.dev/learn/pathway/tutorial/create-an-app,也已经看过将GamePage转成有状态组件(stateful widget)的部分 (https://docs.flutter.dev/learn/pathway/tutorial/stateful-widget)。把onSubmitGuess改成使用setState之后,似乎没有一个提交是起作用的,我还没能弄清原因。你可以在 https://github.com/JLaser1987/Birdle/blob/main/lib/main.dart 查看我的源码。
如果能得到帮助,将不胜感激。
解决方案
其实这个游戏 assert the illegal guess 只有在你提供正确的单词时才会起作用。
extension WordUtils on Word {
//....
Word evaluateGuess(Word hiddenWord) {
assert(isLegalGuess); // this block the update
你可以用 Text 在 _GamePageState 的列中 进行修改
class _GamePageState extends State<GamePage> {
final Game _game = Game();
@override
Widget build(BuildContext context) {
return Padding(
padding: const EdgeInsets.all(8.0),
child: Column(
spacing: 5.0,
children: [
Text(_game._wordToGuess.toString()), /// put this & type exactly this to get the update
for (var guess in _game.guesses)
站内所有文章版权归属LeftHeroAI导航站,无授权禁止任何主体转载、抄袭、复制内容,亦不得私自架设镜像站点。一经侵权,本站将通过法律途径追责。