How to find an item [array] in a dart list - flutter

I have a list with with an array of Objects added with this code:
_selectedItems.add([color, size, type]);
Each time I update the quantity of an item I have to call a method to verify if the list already have that item (if yes, update the quantity. if not, add the item and the quantity). So I tried this code:
print(_selectedItems.contains([color, size, type])); // searching if this product is in the list
But it always return false. I tried too verify the id's of each Object, but it returns false too. What I have to do to be able to reach the goal?

You can use .indexWhere() to find an object in a list and return the index:
Item _objectToInsert = Item();
int index = _selectedItems.indexWhere((item) => item.color.contains(_objectToInsert.color));
If index is -1, the object isn't in the list, else index will be the position of the obect in the list. So you can do:
if (index == -1) // add
_selectedItems.add(_objectToInsert);
else // update
_objectToInsert[index].color = _objectToInsert.color;

Related

FIrebase collection duplicating list

I create an expense collection in firebase but when I access it in other screen using provider every time I add new item It duplicate the previous item as I continue inserting items it duplicate them?
final summaryDataExpense = Provider.of<ExpensesData>(context).expenseList;
final filterByYearExpense = summaryDataExpense
.where((element) =>
DateTime.parse(element['itemDate']).year == currentYear)
.toList();
var totalExpenses = filterByYearExpense.map((e) => e['itemPrice']).toList();
totalExpenses return wrong length of the items existed
You need to replace type of expenseList from List to Set.
Set will take care of the duplicates.

Is there anyway to know where you are in an array? Swift?

I currently have an array of strings. The user will scroll down, and I want to know how far they went down the list.
Is there anyway to do this, without using the indexPath.row feature from tableView/collectionView AND without knowing the string? (String is currently random data pulled from my database)
var name = ["jfiwojf", "awjefoeij", "awjfioe", "awjefoi"]
You can check your table view property indexpathsforvisiblerows. This will return an array with the index of the cells that are visible to the user at the time you call that property. You just need to check the which element of your collection is represented by the last index returned from it.
If are using a collection view you can check its indexpathsforvisibleitems property. Note that the result of this method is not sorted.
I will suggest you to do some things like set an index
You can do it like so
var index = 0
func didTapped(index: Int) {
index = index
}
Then you call when your user selected a row or select an item.

Realm Swift - How to remove an item at a specific index position?

I am storing a simple list of id's as GUIDs in Realm, but would like the ability to delete an object at a particular index position.
So for example, I want to remove 04b8d81b9e614f1ebb6de41cb0e64432 at index position 1, how can this be achieved? Do I need to add a primary key, or is there a way to remove the item directly using the given index position?
Results<RecipeIds> <0x7fa844451800> (
[0] RecipeIds {
id = a1e28a5eef144922880945b5fcca6399;
},
[1] RecipeIds {
id = 04b8d81b9e614f1ebb6de41cb0e64432;
},
[2] RecipeIds {
id = cd0eead0dcc6403493c4f110667c34ad;
}
)
It seems like this should be a straightforward ask, but I can't find any documentation on it. Even a pointer in the right direction would do.
Results are auto-updating and you cannot directly modify them. You need to update/add/delete objects in your Realm to effect the state of your Results instance.
So you can simply grab the element you need from your Results instance, delete it from Realm and it will be removed from the Results as well.
Assuming the Results instance shown in your question is stored in a variable called recipes, you can do something like the following:
let recipeToDelete = recipes.filter("id == %#","04b8d81b9e614f1ebb6de41cb0e64432")
try! realm.write {
realm.delete(recipeToDelete)
}

How to access the items inside the result of my query?

here is a snippet of my code:
using (var uow = new UnitOfWork())
{
//ItemType itself
ItemType itemType1 = uow.ItemTypeRepository.Get(i => i.Name == "ServerTypeList").FirstOrDefault();
Assert.IsTrue(itemType1.ID != null);
var itemType2 = uow.ItemTypeRepository.Get(i => i.Name == "ServerTypeList", orderBy: o => o.OrderBy(d => d.Name), includeProperties: "Items");
//itemType2[0].
...
I am trying to list all the items inside itemType2 ("Get" method returns an IEnumerable):
What's wrong with itemType2.First().Items[0]?
This works because itemType2 is a sequence. Based on what you're seeing in the debugger, it has only one element. If it always has only one element, then you should use itemType2.Single().Items[0] instead.
Every element of this sequence has an Items property, which appears to be a list or array or something else that can be indexed, so once you get there, you can index into it, as above, or you can iterate over it:
foreach (var item in itemType2.Single().Items)
{
// Do something with each item in the sequence
}

Removing from a Collection

I have a DataGridView collection object and check for a particular condition. If it is null, then I remove it from the DataGridView Collection. Here is my code -
foreach(DataGridViewRow dr in myDataGridViewRowCollection.Rows)
{
string title = TypeConvert.ToString(dr.Cells[Name].Value);
if(title == null)
//Remove it from the list.
myDataGridViewRowCollection.Rows.Remove(dr);
}
Now if I have 6 Rows in the myDataGridViewRowCollection and of them, 5 of them have title as null. Now, the above code removes only 3 of the 5 and not the remaining two.
I kind of understand the problem but I am not able to think right now of a solution. Any thoughts?
The problem is that you're changing the myDataGridViewRowCollection.Rows collection as you're iterating over it, which confuses/breaks the iterator. You need to seperate this into two steps. First make a list of what you need to remove, then you can actually remove them.
var toRemove = myDataGridViewRowCollection.Rows.Where(x => x.Cells[Name].Value == null);
foreach(var row in toRemove){
myDataGridViewRowCollection.Rows.Remove(row);
}