Home > other >  Sending multiple controllers as one controller
Sending multiple controllers as one controller

Time:07-11

class OtpScreen extends StatefulWidget {
 const OtpScreen({Key? key}) : super(key: key);

@override
State<OtpScreen> createState() => _OtpScreenState();
}

class _OtpScreenState extends State<OtpScreen> {
   late String otp1;
   late String otp2;
   late String otp3;
   late String otp4;
   late String otp5;
   late String otp6;
   late OtpModel _futureotp;
   final _formKey = GlobalKey<FormState>();
   late TextEditingController controller;

   @override
  void initState() {
  controller = TextEditingController();
  controller.addListener(() {
  _onChanged();
 });
super.initState();
  }

 @override
 void dispose() {
  controller.dispose();
    super.dispose();
   }

  void _onChanged() {
    try {
      dynamic sendotp = ApiHelper().otpValidation();
      if (sendotp is OtpModel) {
        setState(() {
         _futureotp = sendotp;
        });
      }
      if (sendotp.status == 'success') {
        Routes.sailor.navigate(
         '/signIn',
          navigationType: NavigationType.pushAndRemoveUntil,
          removeUntilPredicate: (routes) => false,
          transitions: [SailorTransition.fade_in],
    );
  } else {
    print(sendotp.message);
  }
} catch (e) {
  print(e);
}
  }     

[This is what I'm trying to achieve1

I'm trying to create an OTP screen with textformfield and I have 6 TextEditingControllers but I want to send the value I get from them as one controller. How can I do that? and How would you do it if you were to create an otp screen that sends data to the backend without a button?

p.s: I tried creating 6 controllers and putting an adlistener at the last controller.

CodePudding user response:

If you want to use in-built dart functionality, then try using Expando feature. It basically allows you to attach values to existing objects. Now this may not be answer that you are looking for but it certainly helps to know more than one way to tackle an issue.

If you want to know more about Expando feature than consider checking out this question.

However, you should be careful while adding an object to Expando as they effectively stays alive forever. This is what it says in official docs:

There is no restriction on other classes, even for compile time constant objects. Be careful if adding expando properties to compile time constants, since they will stay alive forever.

CodePudding user response:

you can try pinput, or check their implementation and try to make your own one.

  • Related