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

74 Views Asked by At

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);

.

1

There are 1 best solutions below

0
On
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;
}