Manish sahu
Manish sahu

Reputation: 333

Why TextEditingController not initialize when I get data in getx arguments in Flutter?

I initialize the initial value in TextEditingController but it shows an Error why?.
Error - The instance member 'data' can't be accessed in an initializer.

I am using a StatelessWidget and houseName is under UserRentModel class.


final UserRentModel data = Get.arguments['list'];

final houseNameController = TextEditingController(text: data.houseName);

I tried this code it works, but I think this type of code format is wrong.

final houseNameController =  TextEditingController(text: Get
    .arguments['list'].houseName);

.

Upvotes: 0

Views: 61

Answers (1)

Arbiter Chil
Arbiter Chil

Reputation: 1285

Error - The instance member 'data' can't be accessed in an initializer.

Dart doesn't allow to direct telling that this data is equevalent to data you are passing to the model so to do it needs to be declared separately so try do it like this

final Rx<UserRentModel> data = UserRentModel().obs;

final houseNameController = TextEditingController();
// Put this function on the onInit
loadFirstdata(){
  data.value = UserRentModel.fromJson(jsonDecode(Get.arguments['list'])); // or what kind of data you passing
  update();
  houseNameController.text =  data.value.houseName;
}

Upvotes: 0

Related Questions