skip to Main Content

I want to store the value from textfield in flutter without pressing any button(Like note app in ios)Can anyone helpe me?Thank you

How to get values from tetfield without pressing any button in flutter..
i already used onchange(){} function..but it store every text one by one that user have entered i want to store only the the latest text user entered..

3

Answers


  1.  final myController = TextEditingController();
    
    ...
    
        @override
        void initState() {
          super.initState();
    
          // Start listening to changes.
          myController.addListener(_printLatestValue);
        }
    
        void _printLatestValue() {
         //here you get the latest typed value
          print('${myController.text}');
        }
    
    ...
    
       TextField(
    
          controller: myController,
    

    to get last character from this add String extension after class close ,

    }//class ending closure
    
    extension E on String {
      String lastChars(int n) => substring(length - n);
    }
    

    and your _printLatestValue is like,

     void _printLatestValue() {
             //here you get the latest typed value
              print('${myController.text.lastChars(1)}');
            }
    
    Login or Signup to reply.
  2. TextField(
      onChanged:(text){
        // do sth
      }
    )
    

    onChanged will be called every time the TextField changes.
    You can also use TextEditingController to listen the TextField changes.

    textEditingController.addListener(() {
      // do sth
    });
    
    Login or Signup to reply.
  3. You can make use of the onChanged property of the TextField. The onChanged property is a callback that is invoked every time the text in the TextField changes.Then save the text value using local Db or SharedPreference

    String _textFieldValue = '';
    final SharedPreferences prefs = await SharedPreferences.getInstance();
    
    TextField(
              onChanged: (text) {
                setState(() async {
                  _textFieldValue = text;
                  await prefs.setString('notes_save', _textFieldValue);
                });
              },
            ),
    
    
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search