How to do forEach in flutter - flutter

I am new in flutter. I want to do for each to check my array JSON. I know typescript is doing like this:
Object.keys(res).forEach(key => {
if (res[key].name === 'Max') {
match = true;
}
});
if (match) {
console.log ('Username has been taken');
} else {
console.log('Username is available');
}
My question is for dart language in Flutter, how to do that?

Please find the below sample code for forEach in dart, which can used in flutter also.
main() {
List<String> list = new List<String>();
list.add('apple');
list.add('ball');
list.add('cat');
list.forEach((element) => print(element));
Set<String> set = Set.from(list);
set.forEach((element) => print(element));
}

try this
var decodedData= json.decode(response.body);
for (var item in decodedDat){
if(item['name'] == 'Max'){
// if true
}
}

The Darty way to test whether an Iterable (and by extension List) contains an element that matches a predicate is with any.
bool match = list.any((s) => s == 'Max');

Related

how to using where expression on a list item property that is a list of string at dart

I need a way to search on a list item (list of string) in a list of object. something as below
filtered = allExhibitors.where((element) => element.product_categories == element.product_categories?.where((element2) => element2 == filterModel.categoryId)).toList(growable: true);
allExhibitors is a list of exhibitors (List<Exhibitor>) and product_categories is list of string (List<String>)
Thank you in advance.
You can use contains.
Something like:
class Exhibitor {
List<String> product_categories = [];
}
void f() {
var allExhibitors = <Exhibitor>[];
var filterModelId = '';
var filtered = allExhibitors
.where((element) => element.product_categories.contains(filterModelId))
.toList(growable: true);
}

How to search for 2 different parameters in dart list?

How to search for 2 different parameters in a dart list?
Is there a simple method?
Can I solve the problem using contains?
void _runFilter(String searchKeyword) {
List<Product> results = [];
if (searchKeyword.isEmpty) {
results = allProducts;
} else {
results = allProducts.where(
(element) =>
element.name.toLowerCase().contains(searchKeyword.toLowerCase()) || element.image.toLowerCase().contains(searchKeyword.toLowerCase()),
),
)
.toList();
results = results +
allProducts
.where(
(element) => element.image.toLowerCase().contains(
searchKeyword.toLowerCase(),
),
)
.toList();
}
// refresh the UI
setState(() {
filteredProducts = results;
});
}
You can write all sorts of if-else-combinations in a closure. If you use the {} notation instead of => it will become clearer. Something like this will accomplish what you are looking for:
results = allProducts.where( (element) {
if ( element.name.toLowerCase().contains(searchKeyword.toLowerCase()) {
return true;
} else if ( element.image.toLowerCase().contains(searchKeyword.toLowerCase()) {
return true;
} else {
return false;
}
}).toList();
If this step is clear, you can then try to combine individual statements into a boolean combination via || or && if this looks more convenient in your code.

How to remove typename from graph QL response in flutter

can anyone help me to remove typename and unwanted fields from graph QL from flutter.how to get response as in postman. Iam using the package https://pub.dev/packages/graphql
I had the same issue, I could not find a simple way to disable __typename,
so I wrote a small program,
T removeTypename<T>(T data) =>
_removeUnwantedKeys(data, ['__typename']);
T _removeUnwantedKeys<T>(T data, List keysToRemove) {
if (data is Map && data.containsAnyOf(keysToRemove)) {
Map d = {};
data.forEach((key, value) {
if (!keysToRemove.contains(key)) d[key] = _removeUnwantedKeys(value,keysToRemove);
});
return d as T;
} else if (data is List)
return data.map((c) => _removeUnwantedKeys(c,keysToRemove)).toList() as T;
else
return data;
}
extension MapsMadeEasy<U, V> on Map<U, V>? {
bool containsAnyOf(List keys) {
if (this == null) return false;
return this!.entries.any((element) => keys.contains(element.key));
}
}

Boolean map in flutter

I have boolean position map for example
var position={"isAdmin":true,"isisPleb":false}
I wanna add all true position another list. how can I do this.
You can do this with basic for loop.
List<String> getPosition(Map newMap) {
List<String> positions = [];
for (var i in newMap.entries) {
if (i.value) {
positions.add(i.key);
}
}
return positions;
}
There is also simple way:
List listPosition = [];
position.forEach((key, value) {
if(value==true) listPosition.add(key);
});

How to assign values from storage to a variable in ionic 3

I tried few methods to assign values to variable but could succeed please help.
Method 1:-
getData() {
return this.storage.get('products')
.then(res => {
return this.cart = res;
});;
}
Console.log shows undefined
Method 2:-
cart = [];
getData() {
return this.storage.get('products')
.then(res => {
return this.cart.push(res);
});;
}
Output :
How can i achieve
Cart variable as directly the array list from 0, 1,? [as shown in picture]
Found the Solution
//set Cart Storage
this.storage.get('products').then((data) => {
if (data == null) {
data = [];
}
this.cart = data;//re-initialize the items array equal to storage value
this.cart.push(this.cartItem());
this.storage.set('products', this.cart);
console.log("Cart" + this.cart);
});
On Another Page
// Retrieving data
public getData() {
return this.storage.get('products')
.then(res => {
this.cart = [];
this.cart = res;
console.log(this.cart);
});
}
Try logging the value of the res parameter in the console. From there, you can assign the value of cart to the correct property in the res object.