I have this dart/flutter provider

class Cars with ChangeNotifier {
  List<Car> _items = [];
  Car _selectedCar;
  final Auth auth;

  Cars(this.auth, this._items);

  List<Car> get items {
    return [..._items];
  }

  Car get getCar {
    return _selectedCar;
  }

  void setSelectedCar(Car car) {
    _items.add(car);
    _selectedCar = car; // here I am getting null from _selectedCar
    notifyListeners();
  }
}

Checking with the debugger using Android Studio 3.5.1 I can check that I have in my car variable a real car:

car = {Car} _listeners = {ObserverList} () id = 156 shasi = "FDGFGFDGDFGFD" brand = "Renault" model = "Talisman E1 & E2" nomer = "FGBFDFG"

But when debugger go to line 78 and I see the _selectedCar value is null enter image description here enter image description here


Solution 1: Ovidiu

I've noticed that sometimes, the debugger is actually several lines behind the line highlighted by the IDE. So in your example, line 77 may have not been executed yet, even though line 78 is highlighted.

I don't know exactly why this happens - it's a bit quirky and over time you will get to learn which are the lines that the debugger is never accurate about, and which lines are always accurate. For example, a breakpoint on if (true) { will never be hit. For those reasons, I would recommend avoiding manually stepping over/into once a breakpoint is hit. Instead, click to set breakpoints on all the different lines that you are interested in, and resume running.

For your use case I would suggest putting breakpoints on lines 78 and 79. Closing brackets such as line 79 are always hit.


Solution 2: Marc Ma

Looks like

selected car = car

is an variable inside a class.

Do you have any async method running in the background?

Maybe someone is changing the value, while you look at it.