What will be the displayed text when the user selects the second radio button?
import 'package:flutter/material.dart'; class RadioExample extends StatefulWidget { @override State<RadioExample> createState() => _RadioExampleState(); } class _RadioExampleState extends State<RadioExample> { int _selectedValue = 1; @override Widget build(BuildContext context) { return Column( children: [ Radio<int>( value: 1, groupValue: _selectedValue, onChanged: (value) { setState(() { _selectedValue = value!; }); }, ), Radio<int>( value: 2, groupValue: _selectedValue, onChanged: (value) { setState(() { _selectedValue = value!; }); }, ), Text('Selected: $_selectedValue'), ], ); } }
Check how the _selectedValue changes when a radio button is tapped.
The _selectedValue updates to the value of the tapped radio button. Selecting the second radio button sets it to 2, so the text shows 'Selected: 2'.
Which option correctly fixes the type error in this Flutter radio button snippet?
Radio(
value: 'A',
groupValue: selectedValue,
onChanged: (value) {
setState(() {
selectedValue = value;
});
},
)Radio button value and groupValue must be the same type.
The value is a String ('A'), so selectedValue must also be a String. Casting value in onChanged ensures type safety.
What happens if you call setState outside the onChanged callback after selecting a radio button?
Consider when Flutter rebuilds widgets after state changes.
Calling setState outside the onChanged callback means the state change is not triggered by the user action, so the UI does not update immediately.
In a Flutter app, you want to navigate to a new screen only when a specific radio button is selected. Which code snippet correctly performs this navigation?
Radio<int>( value: 2, groupValue: selectedValue, onChanged: (value) { setState(() { selectedValue = value!; }); // Navigation code here }, )
Check the condition before navigation and use Navigator.push to go forward.
Navigation should happen only if the selected value is 2. Using Navigator.push with a condition inside onChanged ensures correct behavior.
Why does this Flutter radio button not update the UI when tapped?
class MyWidget extends StatefulWidget { @override State<MyWidget> createState() => _MyWidgetState(); } class _MyWidgetState extends State<MyWidget> { int selected = 0; @override Widget build(BuildContext context) { return Radio<int>( value: 1, groupValue: selected, onChanged: (val) { setState(() { selected = val!; }); }, ); } }
State changes must be wrapped in setState to trigger UI rebuild.
Updating the state variable alone does not rebuild the widget. Wrapping the update inside setState tells Flutter to redraw the UI.