Flutter : How check if List contain passes item? - list

I have method when icon on pressed ,
onPressed: () {
check(snapshot.data[index]);
},
and method to check if the data found on another list ,I did as below :
void add_cart(item){
for (var x in list){
if (x.id==item.id){
cartCount = 1;
}
else {
cartCount = 0;
}
}
if (cartCount == 1){
list.remove(item);
}
else{
list.add(item);
}
print(cartCount);
notifyListeners();
}
the cartcount work correct but remove method don't work , what is the true way for that ?

you need to override == and hashcode of your cart model like this
class CartModel {
String id;
// other properties
#override
bool operator ==(Object other) => other is CartModel && runtimeType == other.runtimeType && id == other.id;
#override
int get hashCode => id.hashCode;
}
after that list.remove(item); will work as intended

Related

PCF Not firing updateview without Resize Browser

Created a PCF control, working fine in harnes tool. After integrating to D365 , updateview()not being called. Once I click the developertool then it is working. If am not opening developer tool OR REsize the browser window, then updateview is not being called even though data changed.
Onresetclick am calling notifychanged event , though it is not triggering the updateview
private onResetClick = (newValue: boolean, props: ToggleProps): void => {
if (newValue) {
//isReset true -> clicked set button, action to grey out toggle and change button name to set
if (!this.changeProps.isControlDisabledInForm) {
this._isChanged = true;
this.isReset = this.changeProps.isReset = newValue;
this.changeProps.buttonProperties.checkbox.disabled = true;
this.changeProps.buttonProperties.checkbox.checked = false;
this._selectedOptionID = null;
// this.theNotifyChanged();
}
} else {
if (!this.changeProps.isControlDisabledInForm) {
this._isChanged = true;
this.changeProps.buttonProperties.checkbox.disabled = false;
this.isReset = this.changeProps.isReset = newValue;
this._selectedOptionID
= this.changeProps.optionsetValues.nokey;
// this.theNotifyChanged();
}
}
if (this.theNotifyChanged) {
this.theNotifyChanged();
}
};
/**
* Called when any value in the property bag has changed. This includes field values, data-sets, global values such as container height and width, offline status, control metadata values such as label, visible, etc.
* #param context The entire property bag availabel to control via Context Object; It contains values as set up by the customizer mapped to names defined in the manifest, as well as utility functions
*/
public updateView(context: ComponentFramework.Context<IInputs>): void {
this._context = context;
this.changeProps.isControlDisabledInForm = context.mode.isControlDisabled;
this.changeProps.isVisible = context.mode.isVisible;
this._checkboxID = context.parameters.OptionSetAttribute.attributes?.LogicalName ?? uuidv4();
let selBoolean: boolean = false;
if (!this._isChanged) {
this._selectedOptionID = context.parameters.OptionSetAttribute.raw;
// this.SetButtonProperties(selBoolean);
}
else {
//if (this.changeProps.isReset) {
// // this.changeProps.buttonProperties.checkbox.disabled = true;
// // this.changeProps.buttonProperties.checkbox.checked = false;
//}
//else {
// // this.changeProps.buttonProperties.checkbox.disabled = false;
//}
selBoolean = this.isReset as boolean;//this.changeProps.buttonProperties.checkbox.checked;
}
if (this.optionSetArray) {
for (var i = 0; i < this.optionSetArray.length; i++) {
if (this.optionSetArray[i].Value == this._selectedOptionID) {
this.SelectedOptionSetLabel = this.optionSetArray[i].Label;
}
if (this.optionSetArray[i]?.Label.toUpperCase() === "YES") { //TODO : this needs to be generic not fixed with yes
this.changeProps.optionsetValues.yeskey = this.optionSetArray[i].Value;
} else {
this.changeProps.optionsetValues.nokey = this.optionSetArray[i].Value;
}
}
}
if (!this._isChanged) {
if (this.SelectedOptionSetLabel != null && this.SelectedOptionSetLabel != undefined) {
this.changeProps.buttonProperties.checkbox.checked = this.SelectedOptionSetLabel?.toUpperCase() === "YES" ? true as boolean : false as boolean;//TODO : this needs to be generic not fixed with yes
selBoolean = this.changeProps.isReset = this.isReset = true;
} else {
selBoolean = false;
this.changeProps.isReset = this.isReset = false;
}
}
this._messageContent = selBoolean ? this.changeProps.labels.trueLabel : this.changeProps.labels.falseLabel;
this.changeProps.buttonProperties = {
resetButton: { innerHTML: selBoolean ? "Reset" : "Set" },// when there is a value stored in attribute we need to show Reset
messageContent: this._messageContent,
checkbox: {
disabled: this.changeProps.isControlDisabledInForm ? true : (this.isReset ? false : true),
checked: this.changeProps.buttonProperties.checkbox.checked, checkboxID: this._checkboxID
}
};
console.log("inside ts");
ReactDOM.render(
React.createElement(ToggleButton, this.changeProps
), this.container);
}
Though the context got changed , scope goes to getOutput() method to check if there is any change in output then only PCF view get rerender. In my case, i didnot change the output value in getoutput() method so it was not working.
Here "_selectedOptionID" value should be changed then it started working.
public getOutputs(): IOutputs {
return {
OptionSetAttribute: this._selectedOptionID as number
};
}

Xamarin Forms List Row Values not being updated when Re-LoadingData?

I am creating a list of scanned items. Whenever an item is scanned it checks if it already exists and if it does it increases the quantity, otherwise it will be added as a new item in the list.
When an item is added in the list the change is reflected correctly, however, when only the quantity is changed the value is not visually updated.
if(PurchaseUnloadData.scannedItems.Where(x => x.ItemID == item.ItemID).Count() > 0)
{
PurchaseUnloadData.scannedItems.FirstOrDefault(x => x.ItemID == item.ItemID).Quantity = PurchaseUnloadData.scannedItems.FirstOrDefault(x => x.ItemID == item.ItemID).Quantity + 1;
}
else
{
item.Quantity = 1;
PurchaseUnloadData.scannedItems.Add(item);
}
MessagingCenter.Send<ViewPurchaseUnloadingCart>(this, "ItemsChanged");
In addition when i scan an item twice, meaning the quantity gets to 2, if i add a new row the quantity value is updated! If there isn't a new row it just updates the data behind but not the list.
public class PurchaseUnloadingCartViewModel : ObservableObject
{
private NavigationCategoryData _category;
private PurchaseUnloadData.ScannedItemInfo _selectedItem;
public PurchaseUnloadingCartViewModel()
: base(listenCultureChanges: true)
{
LoadData();
ExportToExcelCommand = new Command(async () => await ExportDataToExcelAsync());
MessagingCenter.Subscribe<ViewPurchaseUnloadingCart>(this, "ItemsChanged", (sender) =>
{
LoadData();
});
}
public ObservableCollection<PurchaseUnloadData.ScannedItemInfo> Items { get; } = new ObservableCollection<PurchaseUnloadData.ScannedItemInfo>();
public NavigationCategoryData Category
{
get { return _category; }
set { SetProperty(ref _category, value); }
}
public PurchaseUnloadData.ScannedItemInfo SelectedItem
{
get { return _selectedItem; }
set
{
if (SetProperty(ref _selectedItem, value) && value != null)
{
Int64 itemID = Convert.ToInt64(value.ItemID);
Application.Current.MainPage.Navigation.PushAsync(new AddScannedItem("EDIT", itemID));
SetProperty(ref _selectedItem, null);
}
}
}
protected override void OnCultureChanged(CultureInfo culture)
{
LoadData();
}
private void LoadData()
{
Category = null;
Items.Clear();
int i = 0;
while (i < PurchaseUnloadData.scannedItems.Count)
{
Items.Add(PurchaseUnloadData.scannedItems[i]);
i++;
}
}
}
Implement INotifyPropertyChanged. Credits to Jason
https://learn.microsoft.com/en-us/dotnet/desktop/winforms/how-to-implement-the-inotifypropertychanged-interface?view=netframeworkdesktop-4.8

Updating Flutter page after setState()

I have a user follow/unfollow page that searches Firebase for a list of followers and returns a Boolean if their usedID is found...
Future<Null> _friends() async {
fb.child("users/${userid}/Following/${id}").onValue.listen((Event event) {
setState((){
if (event.snapshot.value != null) {
friends == true;
} else if (event.snapshot.value == null) {
friends == false;
}
});
});
}
The bool is then set in a separate function that changes a public bool...
bool friends;
The trouble is that my Widget/Scaffold doesn't update here...
friends == true ? new RaisedButton(onPressed: unfollow()) :
new RaisedButton(onPressed: follow());
Any ideas? I'll update with exact code once later.
the issue is that you've used an == (equality) comparison operator instead of = (assignment). Change your code to the following:
Future<Null> _friends() async {
fb.child("users/${userid}/Following/${id}").onValue.listen((Event event) {
setState((){
// this will return true or false which is what you want based on your code :)
friends = event.snapshot.value == null;
});
});
}

How to sort the selected items in a Sitecore Treelist?

Is there a way to always have the selected items in a Sitecore Treelist sorted alphabetically?
No, but you could look in to creating your own 'sorted treelist'. Someone asked a different question earlier today but it has basically the same answer:
Sitecore Tree list datasource - VersionExist
Sitecore lets you create custom field types. They can be based on existing ones, but with some added tweaks.
As mentioned in the answers to the other question, here are 2 articles which are good places to start:
Creating a Composite Custom Field
Apply Dynamic TreeList Source Parameters with the Sitecore ASP.NET CMS
Here's my implementation, which although long, is mostly copy-and-pasted from the decompiled Treelist code. I've highlighted which bits which are new in the Value property and the Add method:
namespace CustomFieldTypes
{
public class Treelist : Sitecore.Shell.Applications.ContentEditor.TreeList
{
public override string Value
{
get
{
// ---------- New code here -----------
var ids = base.Value.Split('|');
var db = Sitecore.Configuration.Factory.GetDatabase("master");
var items = ids.Select(id => db.GetItem(id)).Where(item => item != null);
var orderedItems = items.OrderBy(item => item.Name);
var orderedIds = orderedItems.Select(item => item.ID.ToString());
return String.Join("|", orderedIds);
// ---------------------------------------
}
set
{
base.Value = value;
}
}
protected void Add()
{
if (this.Disabled)
return;
string viewStateString = this.GetViewStateString("ID");
TreeviewEx treeviewEx = this.FindControl(viewStateString + "_all") as TreeviewEx;
Assert.IsNotNull((object) treeviewEx, typeof (DataTreeview));
Listbox listbox = this.FindControl(viewStateString + "_selected") as Listbox;
Assert.IsNotNull((object) listbox, typeof (Listbox));
Item selectionItem = treeviewEx.GetSelectionItem();
if (selectionItem == null)
{
SheerResponse.Alert("Select an item in the Content Tree.", new string[0]);
}
else
{
if (this.HasExcludeTemplateForSelection(selectionItem))
return;
if (this.IsDeniedMultipleSelection(selectionItem, listbox))
{
SheerResponse.Alert("You cannot select the same item twice.", new string[0]);
}
else
{
if (!this.HasIncludeTemplateForSelection(selectionItem))
return;
SheerResponse.Eval("scForm.browser.getControl('" + viewStateString + "_selected').selectedIndex=-1");
ListItem listItem = new ListItem();
listItem.ID = Sitecore.Web.UI.HtmlControls.Control.GetUniqueID("L");
// ----- New Code Here -----------------------
bool listItemAdded = false;
for (int i = 0; i < listbox.Controls.Count; i++ )
{
ListItem control = (ListItem)listbox.Controls[i];
if (control == null)
return;
if (String.Compare(GetHeaderValue(selectionItem), control.Header) < 0)
{
listbox.Controls.AddAt(i, listItem);
listItemAdded = true;
break;
}
}
if (!listItemAdded)
{
Sitecore.Context.ClientPage.AddControl((System.Web.UI.Control)listbox, (System.Web.UI.Control)listItem);
}
// ------------------------------------------
listItem.Header = this.GetHeaderValue(selectionItem);
listItem.Value = listItem.ID + (object) "|" + (string) (object) selectionItem.ID.ToString();
SheerResponse.Refresh((Sitecore.Web.UI.HtmlControls.Control) listbox);
SetModified();
}
}
}
protected static void SetModified()
{
Sitecore.Context.ClientPage.Modified = true;
}
private bool HasIncludeTemplateForSelection(Item item)
{
Assert.ArgumentNotNull((object)item, "item");
if (this.IncludeTemplatesForSelection.Length == 0)
return true;
else
return HasItemTemplate(item, this.IncludeTemplatesForSelection);
}
private bool HasExcludeTemplateForSelection(Item item)
{
if (item == null)
return true;
else
return HasItemTemplate(item, this.ExcludeTemplatesForSelection);
}
private bool IsDeniedMultipleSelection(Item item, Listbox listbox)
{
Assert.ArgumentNotNull((object)listbox, "listbox");
if (item == null)
return true;
if (this.AllowMultipleSelection)
return false;
foreach (Sitecore.Web.UI.HtmlControls.Control control in listbox.Controls)
{
string[] strArray = control.Value.Split(new char[1]
{
'|'
});
if (strArray.Length >= 2 && strArray[1] == item.ID.ToString())
return true;
}
return false;
}
private static bool HasItemTemplate(Item item, string templateList)
{
Assert.ArgumentNotNull((object)templateList, "templateList");
if (item == null || templateList.Length == 0)
return false;
string[] strArray = templateList.Split(new char[1]
{
','
});
ArrayList arrayList = new ArrayList(strArray.Length);
for (int index = 0; index < strArray.Length; ++index)
arrayList.Add((object)strArray[index].Trim().ToLowerInvariant());
return arrayList.Contains((object)item.TemplateName.Trim().ToLowerInvariant());
}
}
}
When this is compiled and in your application you need to go to /sitecore/system/Field types/List Types/Treelist in the core database. In there, you need to fill in the Assembly and Class fields, and clear out the Control fields.
You could create a custom field and sort the items in the selected list using generics, then save the guids back to the field. I covered this in a recent blog post. There isn't that much coding involved.

The correct display name/field for a list item in a Sharepoint list (using Web Services)

I'm creating a program that uses SharePoint Web Services to query and show a Sharepoint list to the user. I can only show one column at a time, so I need to find a 'default column' or 'display column' to show. I know 'Title' is commonly used in many of the content types but I want this to be robust with any type of custom content type or list so I would like to find some way of querying the list to discover this field.
For example: I'm using SharePoint Manager 2010 here and looking at a Link Library (That doesn't have a Title field) but somehow it knows that the list item is called 'http://google.com'. How is it inferring this?
(source: adamburkepile.com)
Looks like DisplayName has quite a bit of logic behind it. Here is the code I got using Reflector:
public string DisplayName
{
get
{
if (!this.IsNew)
{
if ((!this.ParentList.AllowContentTypes && (this.ParentList.BaseType == SPBaseType.DocumentLibrary)) || (this.ParentList.AllowContentTypes && (this.ContentTypeId.IsNonDiscussionFolder || this.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Document))))
{
string str = (string) this.GetValue("BaseName", false);
if (!string.IsNullOrEmpty(str))
{
return SPHttpUtility.HtmlDecode(str);
}
}
SPField fieldByInternalName = this.Fields.GetFieldByInternalName("Title", false);
if (fieldByInternalName != null)
{
string fieldValueAsText = fieldByInternalName.GetFieldValueAsText(this.GetValue(fieldByInternalName, -1, false));
if (!string.IsNullOrEmpty(fieldValueAsText))
{
return fieldValueAsText;
}
}
if (this.ParentList.AllowContentTypes)
{
if (this.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Link))
{
SPFieldUrlValue value2 = new SPFieldUrlValue((string) this.GetValue("URL", false));
if (!string.IsNullOrEmpty(value2.Description))
{
return value2.Description;
}
if (!string.IsNullOrEmpty(value2.Url))
{
return value2.Url;
}
}
if (this.ContentTypeId.IsChildOf(SPBuiltInContentTypeId.Message))
{
Guid discussionTitleLookup = SPBuiltInFieldId.DiscussionTitleLookup;
SPField fld = this.Fields[discussionTitleLookup];
string str3 = fld.GetFieldValueAsText(this.GetValue(fld, -1, false));
if (!string.IsNullOrEmpty(str3))
{
return str3;
}
}
}
if (this.ParentList.BaseType != SPBaseType.Survey)
{
using (IEnumerator enumerator = this.Fields.GetEnumerator())
{
SPField field3;
string str5;
while (enumerator.MoveNext())
{
field3 = (SPField) enumerator.Current;
if (field3.GetFieldBoolValue("TitleField"))
{
goto Label_00C6;
}
}
goto Label_016F;
Label_00BB:
if (!(field3 is SPFieldMultiLineText))
{
return str5;
}
goto Label_00ED;
Label_00C6:
str5 = field3.GetFieldValueAsText(this.GetValue(field3, -1, false));
if (string.IsNullOrEmpty(str5))
{
goto Label_016F;
}
goto Label_00BB;
Label_00ED:
if (str5.Length <= 0xff)
{
return str5;
}
return str5.Substring(0, 0xff);
}
}
SPContext context2 = SPContext.Current;
if ((context2 == null) || (context2.FormContext.FormMode != SPControlMode.Edit))
{
return SPResource.GetString("ViewResponseTitle", new object[] { this.ID.ToString("N0", this.Web.Locale) });
}
return SPResource.GetString("ToolBarMenuRespondToSurvey", new object[0]);
}
SPContext current = SPContext.Current;
if (this.ParentList.BaseType != SPBaseType.Survey)
{
if ((current != null) && current.FormContext.IsNonDiscussionFolder)
{
return SPResource.GetString("ButtonTextNewFolder", new object[0]);
}
return SPResource.GetString("NewFormTitleNewItem", new object[0]);
}
return SPResource.GetString("ToolBarMenuRespondToSurvey", new object[0]);
Label_016F:
return SPResource.GetString("NoTitle", new object[0]);
}
}