Cannot remove value storage ionic 3 - ionic-framework

I would like to remove value in Ionic3 storage but i can't:
item is the id of the station
removeItem(item){
this.storage.get('thestations').then((val) => {
for(var i = 0; i < val.length; i++)
{
if(val[i].id == item) {
this.storage.remove(i);
}
}
});
}
I also tried that to delete the row with the key number 5 but still the same problem:
this.storage.remove('5');

first of all, the storage is a key value store. so when you want to delete an entire item you need to call storage.remove(key)
however when you have an array stored and want to remove an item you need to get the array first, modify it and then save it again.
you cannot work/delete on the storage reference

Related

Flutter list stores values using for loop, but loses these values when used outside the loop

I am trying to read data from a firebase real time database and store it in a list to use in my flutter app.
As seen in the code below, I start by creating a reference to the database. I also create some global variables, where "itemName" stores the name of the item in the database, "itemID" stores the id of each item in the database and "itemNames" is a list of all the item names in the database.
The "activate listeners" method listens to the database, and returns any values if they are changed. Each item ID starts with a J, and continues onto J1, J2, J3 etc. Hence I am using a for loop to access all the item IDs.
The issue I am having is that the itemNames are successfully being stored in the itemNames list, and can be see when I print the list within the for loop (The first print line).
However, when I try print the list value OUTSIDE the for loop, it prints an empty list for loop (second print line).
So in other words, the list is not retaining the elements added to it during the for loop.
Any help would be much appreciated!
final DatabaseReference _dbRef = FirebaseDatabase.instance.ref();
late StreamSubscription _dailySpecialStream;
//Stores the description of each menu item in the DB
String itemName = "";
String itemID = "";
List<String> itemNames = [];
//"Listens for any changes being made to the DB, and updates our app in real time"
void _activateListeners() {
for (int i = 0; i <= 10; i++) {
itemID = "J$i";
_dailySpecialStream =
_dbRef.child("menuItem/$itemID/itemName").onValue.listen((event) {
itemName = event.snapshot.value.toString();
itemNames.addAll([itemName]);
print(itemNames);
});
}
print(itemNames);
}
That is the expected behavior. Data is loaded from Firebase (and most modern cloud APIs) asynchronously, and while that is happening your main code continues to run.
You can most easily see this by placing some print statements:
print('before starting to load data');
for (int i = 0; i <= 10; i++) {
itemID = "J$i";
_dailySpecialStream =
_dbRef.child("menuItem/$itemID/itemName").onValue.listen((event) {
print('loaded data: %i');
});
}
print('after starting to load data');
If you run this, you'll see something like:
before starting to load data
after starting to load data
loaded data: 0
loaded data: 1
loaded data: 2
loaded data: 3
...
So as you can see the after print statement that is lowest in your code, actually printed before any of the data was loaded. This is probably not what you expected, but explains perfectly why the print statement you had outside the loop doesn't print the data: it hasn't been loaded yet!
The solution for this type of problem is always the same: you have to make sure the code that requires the data is inside the callback, or it is called from there, or it is otherwise synchronized.
A simple way to do the latter is by using get() instead of onValue, and then use await on the Future that is returns:
print('before starting to load data');
for (int i = 0; i <= 10; i++) {
itemID = "J$i";
_dailySpecial = await _dbRef.child("menuItem/$itemID/itemName").get();
print('loaded data %i: ${_dailySpecial.value}');
}
print('after starting to load data');
Now with this, the print statements will be in the order you expected.

Flutter - different between the real value and the displayed value

I am facing very strange problem.
I want to implement the option to remove rows from DataTable, and therefore I implemented the following method:
onRemoveRow() {
setState(
() {
lastRowIndex -= selectedGeneLists.length;
geneLists.removeWhere((element) => selectedGeneLists.contains(element));
for (int i = 0; i < geneLists.length; i++) {
GenesListObjIndexed genesListObjIndexed = geneLists[i];
genesListObjIndexed.index = i;
}
selectedGeneLists = [];
},
);
}
This function should modify the list that store the table's data, and the expectation is that when I delete the items from the list the items will be deleted from the table.
But you can see here the following problem (the selected line isn't been removed):
The strange this is when I debug and check the value of the list it's look great and as expected, so what can be the problem?
Use the key property to uniquely identify each row and delete the row with the key value.

Flutter Remove Multiple List Items In For Loop (Multiple Returns In A For Loop)

I have a list of food items. That has a category, subCategory1, subCategory2 ect.
When the user unselects an category item. They are also unselecting the subCategory1, subCatategory2 items etc as they are children of the original categogory item like this:
So I have a for loop that is running through looking for children category list elements and removing them like this:
// Remove a category item and all of its children
if (categoryType == "category") {
List<String> subCategory1Children = List<String>.from(
snapshot.data.documents[gridIndex]['subCategory1Children']);
// Remove the subcategory items
for (int i = 0; i < foodDrinkMenuElements['subCategory1'].length; i++) {
String subID = foodDrinkMenuElements['subCategory1'][i];
int removalIndex = _indexOfListGridElement('subCategory1', subID);
if (subCategory1Children.contains(subID)) {
_removeCategoryGridItem(removalIndex, subID, 'subCategory1');
}
}
//Remove the actual item being pressed
_removeCategoryGridItem(listIndex + 1, id, categoryType);
}
Which calls _removeCategoryGridItem() like this:
void _removeCategoryGridItem(int removalIndex, String id, String categoryType) {
_FoodandDrinkKey.currentState.removeItem(removalIndex,
(BuildContext context, Animation<double> animation) {
return _buildListItem(removalIndex, animation);
});
foodDrinkMenuElements[categoryType].remove(id);
}
The For loop is always finishing after 1 list item is removed. I assume this is because of the return statement in the function _removeCategoryGridItem.
I have seen other answers that say to put it in an list and run through the list but I don't see how that applies here.
Thanks for your help
The For loop is always finishing after 1 list item is removed. I assume this is because of the return statement in the function _removeCategoryGridItem.
No, generally the only way that a function could break out of a loop in a calling function is by throwing an exception.
I don't know what type foodDrinkMenuElements[categoryType] returns, but presuming that it's a List, Map, or Set, you cannot remove items from the collection while you iterating over the collection.
From the List documentation:
It is generally not allowed to modify the list's length (adding or removing elements) while an operation on the list is being performed.... Changing the list's length while it is being iterated ... will break the iteration.
There is similar language for Map and Set.
I have seen other answers that say to put it in an list and run through the list but I don't see how that applies here.
That is exactly what you should do: you should queue which items to remove and then process the queue to avoid mutating the same collection you're iterating over:
final pendingRemoves = List<void Function()>[];
// Remove the subcategory items
for (int i = 0; i < foodDrinkMenuElements['subCategory1'].length; i++) {
String subID = foodDrinkMenuElements['subCategory1'][i];
int removalIndex = _indexOfListGridElement('subCategory1', subID);
if (subCategory1Children.contains(subID)) {
pendingRemoves.add(() =>
_removeCategoryGridItem(removalIndex, subID, 'subCategory1'));
}
}
// Since an index is involved, you also need to remove in reverse order so that
// the queued indices still refer to the same elements.
for (pendingRemove in pendingRemoves.reversed) {
pendingRemove();
}
//Remove the actual item being pressed
_removeCategoryGridItem(listIndex + 1, id, categoryType);

How to wait for item to be removed from list

I am trying to delete and Item from the list, it takes long time in application to delete this element, so I want to wait until this element is deleted and then verify that this item is not present on the page.
this.GroupList = element.all(by.repeater("Group in GroupList | orderBy: order"));
Now I delete an element from the above list based on its Name getAttribute('aria-label'), How can I use ExpectedConditions.stalenessOf OR ExpectedConditions.invisibilityOf to wait till one element from the list is removed from DOM.
If you have a reference to this specific repeater item, you can use stalenessOf Expected Condition, for instance:
// page object
this.GroupList = element.all(by.repeater("Group in GroupList"));
// test
var itemToBeDeleted = pageObject.GroupList.get(5);
// delete an item here
browser.wait(EC.stalenessOf(itemToBeDeleted), 5000);
Note that you don't need the "orderBy" part in the repeater locator.
Another idea would be to wait for the count to decrease by one with a custom expected condition:
function waitForCount(elementArrayFinder, count) {
return function () {
return elementArrayFinder.count(function (actualCount) {
return actualCount === count;
});
};
}
Usage:
pageObject.GroupList.count().then(function (countBefore) {
// delete item here
browser.wait(waitForCount(pageObject.GroupList, countBefore - 1));
});

Filter getElementsByTagName list by option values

I'm using getElementsByTagName to return all the select lists on a page - is it possible to then filter these based upon an option value, ie of the first or second item in the list?
The reason is that for reasons I won't go into here there are a block of select lists with number values (1,2,3,4,5 etc) and others which have text values (Blue and Black, Red and Black etc) and I only want the scripting I have to run on the ones with numerical values. I can't add a class to them which would more easily let me do this however I can be certain that the first option value in the list will be "1".
Therefore is there a way to filter the returned list of selects on the page by only those whose first option value is "1"?
I am pretty sure that there is a better solution, but for the moment you can try something like:
var allSelect = document.getElementsByTagName("select");
var result = filterBy(allSelect, 0/*0 == The first option*/, "1"/* 1 == the value of the first option*/);
function filterBy(allSelect, index, theValue) {
var result = [];
for (var i = 0; i < allSelect.length; i++) {
if(allSelect[i].options[index].value == theValue ) {
result.push(allSelect[i]);
}
}
return result;
}
I managed to get this working by wrapping a simple IF statement around the action to be performed (in this case, disabling options) as follows:
inputs = document.getElementsByTagName('select');
for (i = 0; i < inputs.length; i++) {
if (inputs[i].options[1].text == 1) {
// perform action required
}
}
No doubt there is a slicker or more economic way to do this but the main thing is it works for me.