how to check if a variable was changed in flutter? - flutter

his is my save function
ManageFiles c = MangeFiles();
TextEditingController code = TextEditingController();
autoSave(){
c.saveTXT(code.text);
}
I wish every time a user types a word run and save, but I don't know how to implement it

To save your user's input each time he types anything just simply use onChanged method in your TextField to call your method. It will invoke it each time new value is typed
OR
You can use TextEditingController and add a listener to it. It will be invoked when its value is changed
sample:
late final TextEditingController _textEditing;
#override
void initState() {
_textEditing = TextEditingController();
_textEditing.addListener(() {
final String value = _textEditing.value.text;
// YOUR CODE
});
super.initState();
}

Related

Edit form in flutter with getx

im new for both flutter and getx and i'm trying to make edit Form with getx , but i get Null .. how to fill TextEditingController after api response
class EditMarketerController extends GetxController {
final DashboardApiProvider dashboardApiProvider;
EditMarketerController({required this.dashboardApiProvider});
MarketerToEdit marketer = MarketerToEdit();
TextEditingController usernameController =
TextEditingController();
TextEditingController emailController =
TextEditingController();
#override
void onInit() {
_getMarketer(); //----- **api call**
// Wait until the completion of the data call and then fill ↓↓↓
final usernameController = TextEditingController(text:
marketer.username);
final emailController = TextEditingController(text:
marketer.email);
super.onInit();
}
Use callBack in your _getMarketer method like this.
_getMarketer({Function? callBack}){
/// implement your api and put callBack in your api call success like this
if(callBack != null){
callBack();
}
}

what's the practice which I should follow to declare variables that will get a value from the onInit() without marking them with late keyword

so I have a GetxController, like the following:
class ExampleController extends GetxController {
late TextEditingController textController;
#override
void onInit() {
textController = TextEditingController();
super.onInit();
}
}
this piece of code will work very fine, but as I know, using the late keyword is not recommended use, and removing it, in this case, will cause a dart analyzer null safety lint:
Non-nullable instance field 'emailController' must be initialized.
Try adding an initializer expression, a generative constructor that initializes it, or mark it 'late'.
so is their better options, or ways to do the same without using late or marking the TextEditingController() nullable?

How to update textiew text based on another variable in dart

I have the following code, shown below where every time I make changes to a text field, a function gets called, in my case doSomething(). However, I assumed that since I bound the value of the text to a variable, if that variable were to get updated, the textfield text would also update. This is not happening, and my question is, what would be the simplest way to make the textfield update its text every time the corresponding variable changes.
Edit: Just to clarify, I have no problem getting the doSomething() function to update other parts of the code, I am looking for the inverse where an external variable changes the text of the textfield
import 'package:flutter/material.dart';
class DynamicTextField extends StatefulWidget {
const DynamicTextField(
{
required this.value,
Key? key})
: super(key: key);
final double value;
#override
State<DynamicTextField> createState() =>
_DynamicTextFieldState(value);
}
class _DynamicTextFieldState extends State<DynamicTextField> {
_DynamicTextFieldState(this.value);
final double value;
late TextEditingController textChanged;
#override
void initState() {
textChanged = TextEditingController(text: value.toString());
super.initState();
}
#override
Widget build(BuildContext context) {
return TextField(
controller: textChanged,
onChanged: (text) {
doSomething();
},
);
}
}
you can change the TextField value with the textChanged controller you set to that widget.
String stringVariable = "some value";
textChanged.text = stringVariable;
you can then update the state with a normal SetState(() {}), and it should update the value in TextField
Your value is declared as final. Meaning it can only be set once. Try removing the final declaration.
I also see you’re also declaring the value variable twice, instead of referencing the original value variable.
The second declaration should be in the widgets build method & should = widget.value.
this will update the text everytime DynamicTextField get rebuild.
#override
void initState() {
textChanged = TextEditingController(text: widget.value.toString());
super.initState();
}
Depends how you use the DynamicTextField in another class. The life-cycle of StatefullWidget is call the initState once the widget initialize.

How to pass data in a stateful widget with constructor?

I am new to flutter and I think I miss a little piece of information about constructor and stateful widget. I tried many ways but always have an error. I just want to pass data into my stateful widget to manipulate from there.
Here is my Error
The instance member 'widget' can't be accessed in an initializer.
Try replacing the reference to the instance member with a different expression
Here is my code
class CreateEducatorEventForm extends StatefulWidget {
final DateTime day = DateTime.now();
final String favoriteId = '';
CreateEducatorEventForm(DateTime day, String favoriteId);
#override
_CreateEducatorEventFormState createState() =>
_CreateEducatorEventFormState();
}
class _CreateEducatorEventFormState extends State<CreateEducatorEventForm> {
final _formKey = GlobalKey<FormState>();
bool _isLoading = false;
String _eventName = '';
String _eventDescription = '';
DateTime _eventDateStart = widget.day;
DateTime _eventDateFinish = widget.day;
You can just move it into initState
class _CreateEducatorEventFormState extends State<CreateEducatorEventForm> {
final _formKey = GlobalKey<FormState>();
bool _isLoading = false;
String _eventName = '';
String _eventDescription = '';
DateTime _eventDateStart;
DateTime _eventDateFinish;
#override
void initState() {
super.initState();
_eventDateStart = widget.day;
_eventDateFinish = widget.day;
}
}
To be fair, unless you really need to store this into your state (say, if it really participates in the lifecycle of your widget), you should just refer to it via widget.day whenever you need it.

Flutter, how to dispose textController

I'm learning Flutter and I have in my app, two textFields linked to textControllers in an AlertDialog to get the input from a user as text and display it in cards in the body of the screen. My problem, that I can't solve on my own, is that after I added setState(() {}) in the 'Save' button of the AlertDialog, for the text to acutally get displayed on the screen in body, well after this change the text entered in the TextFields doesn't get cleared aymore after pressing 'Save'.
My Code:
class _HomeScreenState extends State<HomeScreen> {
final TextEditingController titleController = TextEditingController();
final TextEditingController textController = TextEditingController();
DummyDataProvider notes;
#override
void dispose() {
// Clean up the controller when the widget is disposed.
titleController.dispose();
textController.dispose();
super.dispose();
}
The textControllers in question:
MaterialButton(
onPressed: () {
setState(() {
final title = titleController.text;
final text = textController.text;
NoteProvider.insertNote({'title': title, 'text': text});
Navigator.pop(context);
});
What i mean by text not disposing:
https://imgur.com/a/8pyTPM7,
https://imgur.com/a/lr8a3Eh
Thank you in advance!
Why not just use clear()?
final _textController = TextEditingController();
.....
.....
onPressed: () {
_textController.clear();
}
You can reset your text controllers.
For example in onpressed:
titleController = new TextEditingController();
textController = new TextEditingController();
Set state is not required for this.
TextEditingController
Is very expensive object, don't create it often
just use
TextEditingController.text == '';
to clear the text,