getx icon indicating copy to clipboard operation
getx copied to clipboard

flutter getx controller is not registered

Open abdallahboucedraya opened this issue 4 years ago • 3 comments

Describe the bug I create a widget with GetWidget to show auto-generated code from the backend, so I need to pass parameters to the widget than to the controller, but I got instance is not registred

example:

class NRCCodeWidget extends GetWidget<NRCCodeController> {
  final TextEditingController textEditingController;
  final Function onChange;

  const NRCCodeWidget(
      {Key? key, required this.textEditingController, required this.onChange})
      : super(key: key);

  @override
  Widget build(BuildContext context) {
    Get.create(() => NRCCodeController());
    controller.textEditingController = textEditingController;
    controller.onChange = onChange;
    return TextFormField(
      controller: textEditingController,
      readOnly: true,
      style: const TextStyle(
        fontSize: 17.0,
      ),
      onTap: () async {
        controller.getCode();
      },
      textInputAction: TextInputAction.next,
      textAlign: TextAlign.start,
      decoration: kTextFieldDecoration(Icons.code),
    );
  }
}

class NRCCodeController extends GetxController {
  late TextEditingController textEditingController;
  late Function onChange;

  Future<void> getCode() async {
    String? code = await Methods.getCode('CONTACT');
    if (code != null) {
      textEditingController.text = code;
      onChange(code);
    }
  }

  @override
  void onInit() async {
    await getCode();
    super.onInit();
  }

  @override
  void onReady() {
    super.onReady();
  }

  @override
  void onClose() {
    // TODO: implement onClose
    super.onClose();
  }
}

Flutter Version: 2.5 Getx Version: 4.3.8

Describe on which device you found the bug: ex: Samsung J6 Android.

abdallahboucedraya avatar Nov 14 '21 23:11 abdallahboucedraya

`import 'package:flutter/material.dart'; import 'package:get/get.dart';

class NRCCodeWidget extends GetWidget<NRCCodeController> { final TextEditingController textEditingController = TextEditingController(text: "");

NRCCodeWidget({Key? key}) : super(key: key);

@override Widget build(BuildContext context) {

return Material(
  child: TextFormField(
    controller: textEditingController,
    readOnly: true,
    style: const TextStyle(
      fontSize: 17.0,
    ),
    onTap: ()  {
    
    },
    textInputAction: TextInputAction.next,
    textAlign: TextAlign.start,
    // decoration: kTextFieldDecoration(Icons.code),
  ),
);

} }

class NRCCodeController extends GetxController { late TextEditingController textEditingController; late Function onChange;

Future getCode() async { // String? code = await Methods.getCode('CONTACT'); // if (code != null) { // textEditingController.text = code; // onChange(code); // } }

@override void onInit() async { await getCode(); super.onInit(); }

@override void onReady() { super.onReady(); }

@override void onClose() { // TODO: implement onClose super.onClose(); } } `

Shreemanarjun avatar Nov 15 '21 03:11 Shreemanarjun

it may fix.

Shreemanarjun avatar Nov 15 '21 03:11 Shreemanarjun

wrap with GetBuilder fixed this issue on me

tinkokowin-codigo avatar Jul 20 '22 10:07 tinkokowin-codigo