Using Flutter I have a form that contains an AutoCompleteTextField, I am trying to get changing of the value of the text field working. The value to be edited is placed into the AutoCompleteTextField when the form is opened using the technique linked to my previous question:
How to set the initial value of Flutter autocomplete_textfield
Now I find that if I try to change the value of the field the autocompletion appears to work, but the new text does not appear in the form data when the form is submitted, the previous entry remains. I have tried adding the new text to the form data but this does not change the behavior.
The "itemSubmitted" code looks like:
itemSubmitted: (species) {
setState(() {
searchTextField.textField.controller.text = species.commonName;
});
The submit method calls validate and save on the form data:
if (_formKey.currentState.validate() == false) {
return;
}
_formKey.currentState.save();
What more do I need to do to get the new selection in the AutoCOmpleteTextField updated in the firm data?
Update:
It appears that if I start with a blank form I am able to search for an entry in the AutoCompleteTextField, save the data to my database (without clearing the form), search for a new value, and the data is correctly placed into the form data each time. This suggests that the initialization of the field when the form is opened is causing the issue.
Sid
This issue was entirely of my own making, I am using the same code to both create and edit an entry. To differentiate between new and edit I was testing for valid object being passed to the page, my mistake was that when my text search field was updated I was simply checking if there was a valid object had been passed to the page, of course, the new data was in the form data structure and was never being placed into the object being edited.
The solution was to simply first check if there is a valid input object, if there is then a secondary test is made to see if the form data matched the input object, if not the new data is then placed into the input object.
Related
FireStore Array
I already retrieve the vaccine name and the number of count but the problem is the SPACE is also replace or being removed.
Retrived
I WANT TO OUTPUT THE ITEM LIKE THIS
WITH SPACE SAME VALUE IN THE FIRE STORE
I ALREADY GET IT USING THIS CODE BELOW IT I HOPE YOU GET THE POINT OF THE CODE BELOW CLICK THE LINKED TEXT TO SEE IT
IMAGE FOR THE CODE
Basically I used the Character.isDigit, isAlphabetic, isWhiteSpace
then save it to stringbuffer. then save it to my textview
I assigned my buffer variable for text to get also the whitespace.
I am trying to push the form data to the firebase-firestore. And I also did it successfully. But, the problem is that whenever I am trying to submit the form data again and again it just updates the last data with the current data.
Actually, my requirement is that whenever the user hit the submit button. It creates a document with a random id and stores the all data into that specific id that is generated.
You are specifying the document ID in .document() so it'll overwrite the same document. If you want a document with a random ID on every click, try using add() instead as shown below:
val collectionRef = FirebaseFirestore.getInstance().collection("Maintainance")
collectionRef.add(user).addOnCompleteListener(...)
Alternatively, you can leave .document() empty to get a DocumentReference with a random ID:
val userDocument = FirebaseFirestore.getInstance().collection("Maintanance").document() // <-- don't pass an ID
In addition to #Dharmaraj answer:
CASE_1: In a case where you need to track each user's all submitted forms, probably from your explanation you may need to organize each user's form.
Therefore if you need to organize each user's form then create another sub-collection [example: document(userId).collection("USER_FORMS")] within userID document like this:
val documentRef = FirebaseFirestore.getInstance().collection("Maintainance").document(UserUtils.user?.id.toString()).collection("USER_FROMS").document();
CASE_2 : In a case where you need to make your own custom document ID:
1- make a random number or string or any other data type.
2- The random number/string variable must be local to the code block/method that will execute the form submision function.
3- use the number/string generated as the form document Id like this:
//This must be local so as user clicks submision button so as it generates new random number;
val randomFormId = "generateThenumberOrStringAndInitializeTheVariable";
Then use the random number as the form document Id like this:
val documentRef = FirebaseFirestore.getInstance().collection("Maintainance").document(UserUtils.user?.id.toString()).collection("USER_FROMS").document(randomFormId);
I am a bit new to Firebase and so have been playing around with to help myself get more acquainted with it. So while I was playing around with realtime databases, I was trying to append data to the JSON tree. The code is as below
mSaudi.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
count++;
mHistory = mChildRef.child(Integer.toString(count));
current = riyadh;
mChildRef.setValue(riyadh);
mHistory.push().setValue("riyadh");
}
});
The tree which I require is something like this:
value:
1: some text
2: some other text
But what's a actually happening is this:
value:
1: some text
and on updation
value:
2:some text
the previous entry gets erased
I have tried changing the references in various ways but to no avail. Any help in this regard would be appreciated.
If you would like to save both values, you have to save them using a variable such as a Hashmap. If you save a string and then try save another one under the same branch, it will delete everything previously saved. So try the following
HashMap<String, String> map = new HashMap<>();
map.put("1","String");
map.put("2","String");
mHistory.push().setValue(map);
This will save both the strings without deleting one.
If you would only like to add one String
mHistory.push().child("1").setValue("Your first String");
The biggest problem with this though is that everytime you use push() you generate a random key, so you would have to save the key as a string and use it as a reference in your child.
When you set a value on Firebase, it is going to replace everything in, and under the reference.
Let's say that you have a house value, with 2 childs: Color and Size.
If you want to edit only the color value, before the setValue(), you will have to change the reference you are pushing to.
If your reference was getReference().child("houses") and you push something there, it's going to replace everything there and below it. The way to do it is create a new reference (or update the previews one) like this: getReference().child("houses").child(houseKey).child("color") and push your String there.
In your example, you will need to add the field you want to change as a child before the push() method.
The other way was already told by #Janwilx72 and is getting the whole object, updating the value locally and pushing the entire object again.
You can try this
mChildRef.child("2").setValue("some text");
It should be appending new item instead of overwriting them
In Kinetise mobile app generator I want to init form fields (like Text Input) with some dynamic (from web service) values. How can I achieve this?
I found the answer myself so I share it if someone have similar problem:
You need to put Form widget into List widget.
Add List widget and connect it to your API exposing those initial values
Reduce items count to 1, disable SHOW MORE
Add Form widget inside that single-item List widget.
edit TextInput of that Form widget and set its initial value being read from proper node of API you pointed to in step 1.
Initial values of other Form fields should be handled accordingly.
I have created a Parse.com class in which a field type is Array. I save List with the following code:
public void setPreperties(List<String> properties){
put("properties", properties);
}
The are two senarios:
Users will either select one or more properties
Or they will select no properties at all.
If the user has selected properties, its saved with the above code.
When the user deselectes all the properties and click save, I pass null to the above method to save. But this gives NullPointerException.
My question is how can I save empty data in the Array?
Try passing an empty list, like Collections.emptyList() or new ArrayList<String>().