Mike Osborn
Mike Osborn

Reputation: 503

Flutter Error : A value of type 'Object?' can't be assigned to a variable of type 'String'

Getting error when using the slider in flutter.

Code where the error is :

 Slider(
                    value: (_currentStrength ?? userData.strength).toDouble(),
                    activeColor:
                        Colors.brown[_currentStrength ?? userData.strength],
                    inactiveColor:
                        Colors.brown[_currentStrength ?? userData.strength],
                    min: 100.0,
                    max: 900.0,
                    divisions: 8,
                    onChanged: (val) =>
                        setState(() => _currentStrength = val.round()),
                  ),

User 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

Upvotes: 0

Views: 113

Answers (1)

Tim Brückner
Tim Brückner

Reputation: 2099

You are using Flutter with sound null safety enabled. The error message means that you are trying to assign a value which might be null to a parameter which may not be null.

You can probably fix your issue, by adding an exclamation mark to the value you are passing in like: _currentStrength ?? userData.strength!

But please read this: https://dart.dev/null-safety

Upvotes: 1

Related Questions