Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 22 additions & 4 deletions examples/fwe/birdle/lib/main.dart
Original file line number Diff line number Diff line change
Expand Up @@ -69,20 +69,38 @@ class _GamePageState extends State<GamePage> {
// #enddocregion GamePage

// #docregion GuessInput
class GuessInput extends StatelessWidget {
GuessInput({super.key, required this.onSubmitGuess});
class GuessInput extends StatefulWidget {
const GuessInput({super.key, required this.onSubmitGuess});

final void Function(String) onSubmitGuess;

@override
State<GuessInput> createState() => _GuessInputState();
}

class _GuessInputState extends State<GuessInput> {
final TextEditingController _textEditingController = TextEditingController();

final FocusNode _focusNode = FocusNode();

void _onSubmit() {
onSubmitGuess(_textEditingController.text.trim());
_textEditingController.clear();
final text = _textEditingController.text.trim();

if (text.length == 5) {
widget.onSubmitGuess(text);
_textEditingController.clear();
}

_focusNode.requestFocus();
}
Comment on lines 86 to 95
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

high

The _onSubmit method calls onSubmitGuess with the trimmed text without validating its length. Since the Game.guess implementation (via Word.fromString) throws an ArgumentError if the string length is not exactly 5, this will cause the application to crash if a user submits an empty or partially filled input. Adding a length check prevents this crash and allows the user to correct their input before resubmitting.

  void _onSubmit() {
    final text = _textEditingController.text.trim();
    if (text.length == 5) {
      widget.onSubmitGuess(text);
      _textEditingController.clear();
    }
    _focusNode.requestFocus();
  }


@override
void dispose() {
_textEditingController.dispose();
_focusNode.dispose();
super.dispose();
}

@override
Widget build(BuildContext context) {
return Row(
Expand Down
Loading