Home > database >  Null check operator used on a null value in flutter,dart
Null check operator used on a null value in flutter,dart

Time:10-02

this error :

Null check operator used on a null value

happens when we use bang operator (!) on a nullable instance which wasn't initialized.

  String? _currentUser;
  Map? _userData;

  String? userName;
  String? userLastName;
  double? userFiatAsset;


  List cryptoAsset = <double>[];
  Future<void> getUserData() async {

    _currentUser = await FirebaseAuth.instance.currentUser!.email.toString();
    print(_currentUser);

    await FirebaseFirestore.instance
        .collection('users')
        .doc(_currentUser)
        .get()
        .then((value) => _userData =  value.data());

    if (_userData != null) {
      print(_userData!['Name']);
      print(_userData!['LastName']);
      print(_userData!['FiatAsset']);

       userName = _userData!['Name'];
       userLastName = _userData!['LastName'];
       userFiatAsset = _userData!['FiatAsset'];

    }
  }

when I perform this the mentioned error pops up. when I comment the line

userName = _userData!['Name'];

the error is solved, so I understand that the error is from this line.. but my previous prints work well and return the desire value:

print(_userData!['Name']);

so how can this happen? I have a valuable that is not null but the error shows something else.. what is wrong?

CodePudding user response:

Try the following code:

  String? _currentUser;
  Map? _userData;

  String? userName;
  String? userLastName;
  double? userFiatAsset;


  List cryptoAsset = <double>[];
  Future<void> getUserData() async {

    _currentUser = await FirebaseAuth.instance.currentUser!.email.toString();
    print(_currentUser);

    await FirebaseFirestore.instance
        .collection('users')
        .doc(_currentUser)
        .get()
        .then((value) => _userData =  value.data());

    if (_userData != null) {
      print((_userData ?? {*the value you want to replace when returning null to you})['Name']);
      print((_userData ?? {*the value you want to replace when returning null to you})['LastName']);
      print((_userData ?? {*the value you want to replace when returning null to you})['FiatAsset']);

       userName = (_userData ?? {*the value you want to replace when returning null to you})['Name'];
       userLastName = (_userData ?? {*the value you want to replace when returning null to you})['LastName'];
       userFiatAsset = (_userData ?? {*the value you want to replace when returning null to you})['FiatAsset'];

    }
  }

CodePudding user response:

remove ?, it explicitly states that a variable/parameter may be null.

String _currentUser;
Map _userData;
String userName;
String userLastName;
double userFiatAsset;

You can use late keyword when your parameters will be assigned soon:

late Map _userData;

When you use ! it means conversion from a nullable to a non-nullable type. Use it only if you are absolutely sure that the value will never be null. You can remove ! it when your sure its not null

userName = _userData['Name'];

BTW, you check for userData, which means it wont be null

if (_userData != null) {} // this condition is enough for this

null safety in dart

You can also use ?? when it returns null assign new value to it:

_userData['Name'] ?? // value when its null

what-is-the-dart-null-checking-idiom

  • Related