How to delete a field in Firestore document with flutter - dart

I'm making a Flutter application.
But, I cannot delete a field in the Firestore document.
In another language I know to use FieldValue.delete() to delete a file in Firestorm document.
In Dart, How do I delete?

Update Oct,2018: This is Now Possible:
In order to delete a particular field from a Cloud Firestore document - make sure you are using Plugin version 0.8.0 or Above. Now a E.g If you have a document having a field 'Desc' with contain some Text. In Order to Delete it.
Firestore.instance.collection('path').document('name').update({'Desc': FieldValue.delete()}).whenComplete((){
print('Field Deleted');
});
This will Delete 'Desc' Field from the Document 'name'

I think this is currently impossible in standard, non hacky way.
There is an open issue https://github.com/flutter/flutter/issues/13905 in Flutter which have to be resolved first.

Get the DocumentReference, invoke update with a map which has a key you want to delete and value FieldValue.delete().
var collection = FirebaseFirestore.instance.collection('collection');
collection
.doc('document_id')
.update({
'field_to_delete': FieldValue.delete(),
}
);

Here is an official solution for deleting Field Path when programming in Flutter/Dart
Here is how a sample Firestore collection looks like:
{
'path': {
'name': {
'title': 'my title'
'description': 'my description',
}
}
}
In order to delete description field, you can do this:
Firestore.instance.collection('path').document('name').set(
{'description': FieldValue.delete()},
SetOptions(
merge: true,
),
)
And the collection will look like this:
{
'path': {
'name': {
'title': 'my title'
}
}
}

if you have nested fields then use the '.' (Dot) notation to specify the field.
E.g if your data is nested Map then this is handy.
Firestore.instance.collection('path').document('name').update({'address.town': FieldValue.delete()}).whenComplete((){
print('Field Deleted');
});

Related

Extra trigger output caused by sample data

I've used Zapier CLI to create a trigger and it was working great (trigger output looked great, no unwanted fields present). When I submitted the integration to Zapier, they required each trigger to include the sample property:
perform: perform,
performList: performList,
sample: {
data: {
items: [
{
item: {
id: '42c21e82-0772-4d79-a6b3-c916e51b24ff'
},
language: {
id: '00000000-0000-0000-0000-000000000000'
}
}
]
},
//etc..
Since adding this, the trigger's output contains duplicated entries- one populated and one empty:
I'm not sure how to get rid of this.. any ideas? Note: This occurs in triggers where I am and am not using outputFields.
I found the cause- the data in sample was slightly different from the data output by performList.

How i can on October CMS use translate plugin after extend new fields to user plugin?

For my project i use User Plugin and Translate Plugin.
I've added custom new fields to the user and now I want to translate them.
I think I know why it does not work. But find no solution.
Somebody to have idea?
if i add to $model->translatable default fields like 'email', works fine.
i added boot function to my custom plugin with this code
\RainLab\User\Models\User::extend(function ($model) {
$model->implement[] = 'RainLab.Translate.Behaviors.TranslatableModel';
$model->translatable = ['about', 'preview_text'];
});
Hmm,
There is one problem. when you try to add it directly $model->translatable, It seems its treating it like attribute of model.
Try this $model->addDynamicProperty(variable_name, value);
\RainLab\User\Models\User::extend(function ($model) {
$model->implement[] = 'RainLab.Translate.Behaviors.TranslatableModel';
$model->addDynamicProperty('translatable', ['about', 'preview_text']);
// like this ^
});
It should treat it as local variable and it should work.
If any doubts please comment.
Revision [ Final solution ] - solution for : it works for existing fields when we are adding new fields this is not working.
Problem: With translation mechanism is that it listens the backend.form.extendFieldsBefore event for form and then register fields for translation. When we try to register new fields in form using extendFormFields extension, It happens afterward so new added fields are not visible to translation listener. so they are kind of skipped as translation field registration process already been done.
Solution: So for solution we can just add our field before translation registartion happens. luckily translate plugin has lowest -1 priority for listening this event backend.form.extendFieldsBefore so we can register our fields before It so we are good now and our fields can be added before it can process fields for translation.
Code
\Event::listen('backend.form.extendFieldsBefore', function($widget) {
// You should always check to see if you're extending correct model
if (!$widget->model instanceof \RainLab\User\Models\User) {
return;
}
// we will merge current fields with fields we want to add
// we used shorthand + plus operator for this
$widget->tabs['fields'] = $widget->tabs['fields'] + Config::get('rms.secis::user_fields');
// here Config::get('rms.secis::user_fields') is just returning field array
// Fo ref. Ex:
// return [
// 'gender' => [
// 'label' => 'Gender',
// 'tab' => 'Security Island',
// 'type' => 'radio',
// 'options' => [
// 'male' => 'Male',
// 'female' => 'Female'
// ],
// 'span' => 'auto'
// ],
// ];
});
Note: we are adding fields to tab so we are using $widget->tabs['fields'] to add fields to the tabs. If you want to add normal fields or secondary tab fields you can use $widget->fields and $widget->secondaryTabs['fields] respectively.
Yes now translator can see our fields and its processed, It should able to show translation UI in frontend-ui as well.
if any doubts please comment.
#hardik-satasiya
yes, no more errors on frontend, but new problem is, that no translate functions on fields.
Maybe to add jQuery Script to Controller?
Integration without JQuery and October Framework files:
https://octobercms.com/plugin/rainlab-translate
end of documentation

Create Remote Select2 v4 Option with custom parameters

here is the problem, I can create the custom option as specify on documentation like this:
var option = new Option("my custom option", "myid", true, true);
$('select[name="myselect"]').append(option).trigger('change');
// manually trigger the `select2:select` event
$('select[name="myselect"]').trigger({
type: 'select2:select',
params: {
data: {
id: "myid",
text: "my custom option",
customparam: {
hello: "I'm here"
}
}
}
});
So, this creates the option normally and shows on select2 as selected as expected, this also trigger the select2:select event and I can read the extra parameter when this event is trigger, but here comes the problem, I can NOT access the extra parameter by doing this:
$('select[name="myselect"]').select2('data')[0].customparam
it's like the custom parameter is not attached to the element and it was only pass to the events.
for all who try to find an solution to add the custom parameters back on remote select2, here is what I did and worked perfectly:
$('select[name="yourfieldname"]')
.append(new Option(`${doc.n}`, doc._id, true, true))
.select2('data')[0].customparam = doc.i;
then just when you need call:
$('select[name="yourfieldname"]').select2('data')[0].customparam
The method described by #rafaelrglima's works for me, but I prefer the method below, which was gleaned from dejan9393's answer in Github issue 2830:
var dataAdapter = $('select[name="myselect"]').data('select2').dataAdapter;
dataAdapter.addOptions(dataAdapter.convertToOptions([{
id: "myid",
text: "mycustomoption",
customparam: "hello"
}]));
I prefer this way because I don't have to have one step for adding "id" and "text" and separate step for adding custom parameters. Also convertToOptions takes an array, so you have a convenient way to add multiple options in one step.

Config data bindings in Input Autocomplete - ElementUI

Default, attributes 'value' is key for binding value. And if i want to changes data bindings from 'value' to 'name', it not worked!
[Example](https://jsfiddle.net/vu_1995/qkf549vL/3/)
You can change the key that el-autocomplete uses by specifying the attribute value-key="name"
Also, in your fiddle, you also have to make sure you're using name instead of value in the filter function:
createFilter(queryString) {
return (link) => {
return (link.name.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
};
},
Here's an updated fiddle: https://jsfiddle.net/jmbldwn/tLyc7hxg/1/

"select2" Add constant option

I am currently using Select2 in a project and would like to add a option to my select list that shows up regardless of what the user types or searches. The idea is to have a "Add new" option always present in the list.
I do not think my code is necessary here (but if needed I may provide) as the only thing i'm lacking knowledge in this specific topic is on how to keed the option always showing.
I thought of using the matcher attribute, but i'm not sure how.
I've managed to do it setting a new matcher, the problem was I was not sure on how to create a new matcher and still use the select2 default one.
Something else I was missing was the full version of select2.
function newMatcher(term, text){
//The "ADD NEW" String is the text in the option I want to always show up.
//The code after OR looks for the actual results for the user's search
if ((text.toUpperCase().indexOf("ADD NEW") > -1)
|| (text.toUpperCase().indexOf(term.toUpperCase()) > -1)) {
return true;
}
}
$(document).ready(function() {
$.fn.select2.amd.require(['select2/compat/matcher'], function (oldMatcher) {
$('select').select2({
matcher: oldMatcher(newMatcher)
})
})
});

Resources