Flutter Error : The left operand can't be null, so the right operand is never executed. Try removing the operator and the right operand
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;
Mike Osborn
Updated on December 31, 2022Comments
-
Mike Osborn over 1 year
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 });
-
Yeasin Sheikh almost 3 yearshow about using
_currentSugars!=null? _currentSugars: userData.sugars
-
Midhun MP almost 3 yearsYou declared
_currentSugars
as non nullable. Declare it asString? _currentSugars;
-
Mike Osborn almost 3 years@YeasinSheikh Yeah but it show blue line relating unnecessary null comparison : "The operand can't be null, so the condition is always true. Remove the condition."
-
Yeasin Sheikh almost 3 yearsits; ok to use like this, but message is showing because your
currentSugars
will never receive null value, also you can remove conditional statement. -
Mike Osborn almost 3 years@MidhunMP it says: "Conditions must have a static type of 'bool'. Try changing the condition." and the same error: "The left operand can't be null, so the right operand is never executed. Try removing the operator and the right operand."
-
Midhun MP almost 3 years@MikeOsborn Could you please show how you changed the condition and how you declared that variable now ?
-
Mike Osborn almost 3 yearsYea see my update screenshot
-
Midhun MP almost 3 years@MikeOsborn I mean variable declaration. Instead of
late String _currentSugars;
declare it asString? _currentSugars;
-
Mike Osborn almost 3 yearsBruh it also works
-
Mike Osborn almost 3 yearsAnyway thanks for the help
-
Mike Osborn almost 3 years@MidhunMP can you also answer this question if you want to.stackoverflow.com/questions/68449448/…
-
-
Mike Osborn almost 3 yearsCan u help me with this stackoverflow.com/questions/68493717/…