Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Calling a function onSubmit TextField does not work

I have created a class for new Transaction entry form.. where 2 TextField and a Button..

I have created a Function named myf(), I am almost successful but to improve my knowledge and better coding…. I wish to call function like pointer..but it does not work…where I am getting wrong?

I can solve by repeating code inside onsubmit but its not good level job…

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

POINTER WORKS WITH TEXTBUTTON AND DOES NOT WORK WITH TEXTFIELD ONSUBMIT..THIS IS MY CONFUSION…

here is my coding

import 'package:flutter/material.dart';
class NewTransaction extends StatelessWidget {
  final Function func;
  NewTransaction(
    this.func
  );
  void myf() {
    func(txttitle.text,double.parse(txtamount.text));
  }
  final txttitle = TextEditingController();
  final txtamount = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: const EdgeInsets.symmetric(vertical: 20.0,horizontal: 8.0),
      child: Card(
        shape: RoundedRectangleBorder(
            borderRadius: BorderRadius.circular(10.0)),
        elevation: 10,
        child: Container(
          padding: EdgeInsets.all(10),
          child: Column(
            children: [
              TextField(
                controller: txttitle,
                onSubmitted: (_){
                  myf();
                },
                decoration: InputDecoration(labelText: 'Enter Title'),
              ),
              TextField(
                onSubmitted: (_)=>myf, // this one is not working
                keyboardType: TextInputType.number,
                  controller: txtamount,
                  decoration: InputDecoration(labelText: 'Enter Amount')),
                  TextButton(
                  onPressed: myf,
                  child: Text('Add Record'))
            ],
          ),
        ),
      ),
    );
  }
}

>Solution :

While we do ()=>MyFunc() we create an anonymous(/inline) function.

onSubmitted: (_)=>myf, means , when onSubmitted will trigger, just return myf, here myf is just a variable, But we need to call the method, that’s why you need to use onSubmitted: (_)=>myf()

A better way will be creating myf with parameter,

void myf(String value){}

And use

onSubmitted: myf,

Here is a great answer about inline function and you can check others answers.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading