'The method '[]' can't be unconditionally invoked because the receiver can be 'null' what is the problem

I want to show the staff list in the Firebase database. However, I am getting the following error.

import 'package:firebase_database/firebase_database.dart';

class Drivers
{
String? name;
String? phone;
String? email;
String? id;
String? car_color;
String? car_model;
String? car_number;

Drivers({this.name, this.phone, this.email, this.car_color, this.car_model, this.car_number});

Drivers.fromSnapshot(DataSnapshot dataSnapshot)
{
id = dataSnapshot.key;
name = dataSnapshot.value["name"];
phone = dataSnapshot.value["phone"];
email = dataSnapshot.value["email"];
car_color = dataSnapshot.value["car_color"];
car_model = dataSnapshot.value["car_model"];
car_number = dataSnapshot.value["car_number"];
}
}

What is the problem with my code??

I tried to put ! symbol after .value but nothing is happen



Solution 1:[1]

Try using typecast instead of using '!' .

You can try casting to dynamic. as,

(dataSnapshot.value as dynamic)["name"];

Solution 2:[2]

You need to convert object into map before using indexer ([]) on it. Try the code block below

import 'package:firebase_database/firebase_database.dart';

class Drivers
{
String? name;
String? phone;
String? email;
String? id;
String? car_color;
String? car_model;
String? car_number;

Drivers({this.name, this.phone, this.email, this.car_color, this.car_model, this.car_number});

Drivers.fromSnapshot(DataSnapshot dataSnapshot)
{
id = dataSnapshot.key;
name = (dataSnapshot.value as Map)["name"];
phone = (dataSnapshot.value as Map)["phone"];
email = (dataSnapshot.value as Map)["email"];
car_color = (dataSnapshot.value as Map)["car_color"];
car_model = (dataSnapshot.value as Map)["car_model"];
car_number = (dataSnapshot.value as Map)["car_number"];
}
}

Solution 3:[3]

The DataSnapshot value can be null so you can not directly try to use the field name.

You should access it using the null operator "!"

Example:

Drivers.fromSnapshot(DataSnapshot dataSnapshot)
{
  id = dataSnapshot.key;
  name = dataSnapshot.value!["name"];
}

Solution 4:[4]

If you check the reference documentation for DataSnapshot.value, you'll see that it returns Object?. This means that it either returns an Object or it returns null. Flutter/Dart requires that you explicitly handle that null nowadays.

The simplest way is to check if the data snapshot exists, and then force Flutter to unwrap it:

Drivers.fromSnapshot(DataSnapshot dataSnapshot) {
  if (dataSnapshot.exists) {
    id = dataSnapshot.key;
    Map data = dataSnapshot.value! as Map;
    name = data["name"];
    phone = data["phone"];
    email = data["email"];
    car_color = data["car_color"];
    car_model = data["car_model"];
    car_number = data["car_number"];
  }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shanu
Solution 2 Alind Sharma
Solution 3 suzan
Solution 4 Frank van Puffelen