I am trying to build my flutter project but I get an error while using the ternary operator. I am using the ternary operator because it would check if the first statement is null, if it is null then it's going to use the second statement. This helps me with my code but I am getting an error, it states :
The left operand can't be null, so the right operand is never executed. Try removing the operator and the right operand.
Example:
value: initial_value ?? current_value
Code :
class SettingsForm extends StatefulWidget {
@override
_SettingsFormState createState() => _SettingsFormState();
}
class _SettingsFormState extends State<SettingsForm> {
final _formKey = GlobalKey<FormState>();
final List<String> sugars = ['0', '1', '2', '3', '4'];
final List<int> strengths = [100, 200, 300, 400, 500, 600, 700, 800, 900];
// form values
late String _currentName;
late String _currentSugars;
late int _currentStrength;
@override
Widget build(BuildContext context) {
MyUser user = Provider.of<MyUser>(context);
return StreamBuilder<UserData>(
stream: DatabaseService(uid: user.uid).userData,
builder: (context, snapshot) {
if (snapshot.hasData) {
UserData? userData = snapshot.data;
return Form(
key: _formKey,
child: Column(
children: <Widget>[
Text(
'Update your brew settings.',
style: TextStyle(fontSize: 18.0),
),
SizedBox(height: 20.0),
TextFormField(
initialValue: userData!.name,
decoration: textInputDecoration,
validator: (val) =>
val!.isEmpty ? 'Please enter a name' : null,
onChanged: (val) => setState(() => _currentName = val),
),
SizedBox(height: 10.0),
DropdownButtonFormField(
value: _currentSugars ?? userData.sugars, <--Error : **userData.sugars**
decoration: textInputDecoration,
items: sugars.map((sugar) {
return DropdownMenuItem(
value: sugar,
child: Text('$sugar sugars'),
);
}).toList(),
onChanged: (val) => setState(() => _currentSugars = val),
),
SizedBox(height: 10.0),
Slider(
value: (_currentStrength ?? userData.strength).toDouble(), <--Error : **userData.strength**
activeColor:
Colors.brown[_currentStrength ?? userData.strength], <--Error : **userData.strength**
inactiveColor:
Colors.brown[_currentStrength ?? userData.strength], <--Error : **userData.strength**
min: 100.0,
max: 900.0,
divisions: 8,
onChanged: (val) =>
setState(() => _currentStrength = val.round()),
),
ElevatedButton(
style:
ElevatedButton.styleFrom(primary: Colors.pink[400]),
child: Text(
'Update',
style: TextStyle(color: Colors.white),
),
onPressed: () async {
if (_formKey.currentState!.validate()) {
await DatabaseService(uid: user.uid).updateUserData(
_currentSugars ?? snapshot.data!.sugars, <--Error : **snapshot.data!.sugars**
_currentName ?? snapshot.data!.name, <--Error : **snapshot.data!.name**
_currentStrength ?? snapshot.data!.strength); <--Error : **snapshot.data!.strength**
Navigator.pop(context);
}
}),
],
),
);
} else {
return Loading();
}
});
}
}
'MyUser' data class :
class UserData {
final String uid;
final String name;
final String sugars;
final int strength;
UserData({ required this.uid, required this.sugars, required this.strength, required this.name });
Screenshot Update
You've declared the _currentSugars
variable as a non-nullable string. So you are explicitly stating that it cannot contain a null value. By adding late keyword to that, compiler can't ensure the null-safety constraint at compile time.
As per late-variables
The late modifier means “enforce this variable’s constraints at runtime instead of at compile time”
So instead of declaring it as:
late String _currentSugars;
Declare it as:
String? _currentSugars;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With