user54517
user54517

Reputation: 2420

Flutter - Error: The getter X isn't defined for the class

I have a class TripController that contains a private field _updatedAccount. I created a getter in order to get from outside.

class TripController {
  final String _accountId;
  final BuildContext _context;
  Account _updatedAccount;  
  Account updatedAccount() => _updatedAccount;


  TripController(this._accountId, this._context);
...
}

In another class, where I perfectly have access to the TripController class, I have the code :

onTap: () {
 TripController _tripController =
 new TripController(_account.id, context);
 _tripController.add(context);
 _account.trips  = _tripController.updatedAccount.trips;
 _account.notifyListeners();
},

And here, updatedAccount from _tripController.updatedAccount.trips is underlined in red with the message : The getter 'updatedAccount' isn't defined for the class 'TripController'

Did I misdeclared the getter ?

Upvotes: 15

Views: 72882

Answers (8)

chujudzvin
chujudzvin

Reputation: 1303

I faced a similiar issue, but with a library syncfusion_flutter_calendar. The problem was a breaking change, that required either upgrading flutter or downgrading the library.

State in which it didn't work was: Flutter v.32.16.5 + syncfusion_flutter_calendar v.24.2.6.

After upgrading flutter to 3.22.2 everything worked.

Upvotes: 0

Yuriy N.
Yuriy N.

Reputation: 6087

The first thing to do when getting an exception in Flutter - is to restart IDE.

Upvotes: 0

Ghazal
Ghazal

Reputation: 383

I faced this error and the problem was my IDE, not my code. This solution worked for me in Android Studio :

File -> Invalidate Catches... -> Invalidate And Restart

Upvotes: 3

spehj
spehj

Reputation: 1

Check your imported libraries on the top of your file where the problem is. In my case, Android Studio auto-imported an object from the core Flutter library with the same name as the model I've defined on my own.

I just deleted the import and then imported my own object from my_models.dart file.

Upvotes: 0

Aizat Ashaari
Aizat Ashaari

Reputation: 1

Try to check the model class name you use might the same with existing name of Widget or Plugin

Upvotes: 0

user54517
user54517

Reputation: 2420

Okay, I finally fixed it. I don't know why, but I had to delete the code related to TripController, and ther re-write it again. I don't know why, maybe it was an Editor problem, I'm using VScode.

Upvotes: 7

Evrard-Nil Daillet
Evrard-Nil Daillet

Reputation: 105

You are using the classic method syntax declaration, in Dart language, prefer using this kind of syntax for getters:

Account get updatedAccount => _updatedAccount;

And call it the way you did. Else you should call it like a classic method:

_tripController.updatedAccount().trips

Please follow this link for further information:

https://dart.dev/guides/language/language-tour#getters-and-setters

Upvotes: 0

Tidder
Tidder

Reputation: 1214

You have declared updatAccount() as a method, not as a getter. Use _tripController.updatedAccount().trips; or change the method to a getter Account get updatedAccount => _updatedAccount;

Upvotes: 3

Related Questions