简体   繁体   中英

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

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! _currentStrength ?? userData.strength!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM