Flutter - Search elements in array with firstwhere - dart

The problem is simple: What is the correct way to search element in array ?
My code is
data = [{id: 1, descripcion: Asier}, {id: 2, descripcion: Pepe}]
estateSelected= data.firstWhere((dropdown)=>dropdown.id==1);
The error that return is
Bad state: no element

You have some errors, this should work:
var data = [{'id': 1, 'descripcion': 'Asier'}, {'id': 2, 'descripcion': 'Pepe'}];
var estateSelected = data.firstWhere((dropdown) => dropdown['id'] == 1);
print(estateSelected);
Fastest way to try is on dartpad

Related

Is it possible to remove duplicates in Dart with another variable

I have searched a lot for removing duplicates from a list in Dart using ANOTHER variable.
Here is what I mean:
List<int> numbers = [1, 2, 3, 4];
// This list has 4 new elements than the first one
List<int> moreNumbers = [1, 2, 3, 4, 5, 6, 7, 8];
// Now I want to push the moreNumbers unique elements to the numbers one
I want to push it so the end result for the numbers variable should be:
[1, 2, 3, 4, 5, 6, 7, 8];
Is it possible?
void main() {
var lst = [1,2,3,4];
var lst2 = [1,2,3,4,5,6,7,8];
var s = {...(lst+lst2)};
print(s.toList());
}
The trivial approach would be:
for (var number in moreNumbers) {
if (!numbers.contains(number)) {
numbers.add(number);
}
}
Not particularly efficient if numbers is long, because contains on a list can take time proportional to the length of the list.
The time/space trade-off would be creating a set from numbers, because sets have cheap contains:
var alsoNumbers = numbers.toSet(); // Also linear, but only happens once.
for (var number in moreNumbers) {
if (alsoNumbers.add(number)) { // true if elements was added
numbers.add(number);
}
}
(Using add instead of contains ensures that you update the set with new values, so you won't add the same new value twice.)
If you could just make numbers a Set to begin with, it would be much easier to avoid duplicates, just do numbers.addAll(moreNumbers).

How to insert a list into a map?

I have 2 data one in an array and the other in a list. I need to send these data in API but combine them.
My first data look like this:
data1 = {vendorId: 17, vendorName: Dolphin Bakers};
and second is like this
data2 = [
{id: 3, province: and, code: 56201},
{id: 3, province: and, code: 56201},
];
I need to send it like this
{
vendorId: 17,
vendorName: Dolphin Bakers,
data: [
{id: 3, province: and, code: 56201},
{id: 3, province: and, code: 56201},
],
}
I am trying to add second data in the first data
data1.add(data: data2);
Its showing error Class '_InternalLinkedHashMap<String, dynamic>' has no instance method 'add'.
You must put a "data" key in the map:
data1["data"] = data2;

Is there a Dart equivalent of Python's all() method?

I'm looking for a method that returns true if a function test, given an element, returns true for every element of an Iterable, or in my case a List, similar to Python's all() method.
I think what you are looking is the every method.
var numbers = [1, 2, 3, 4, 5];
var evenNumbers = [2, 4, 6, 8, 10];
print(numbers.every((n) => n.isEven)); //false
print(evenNumbers.every((n) => n.isEven)); //true
Iterable#every
Checks whether every element of this iterable satisfies test.
void main() {
print([1, 2, 3].every((i) => i > 0));
}

Does typeorm support SQL IN clauses

Does typeorm support SQL IN clauses? I'm trying to query a repository where a field matches 1 of multiple values.
myRepository.find({
where: {
SomeID: // IN [1, 2, 3, 4]
}
});
You can use QueryBuilder for this purpose:
const users = await userRepository.createQueryBuilder("user")
.where("user.id IN (:...ids)", { ids: [1, 2, 3, 4] })
.getMany();
You can now do this (from the docs):
import {In} from "typeorm";
const loadedPosts = await connection.getRepository(Post).find({
title: In(["About #2", "About #3"])
});
will execute following query:
SELECT * FROM "post" WHERE "title" IN ('About #2','About #3')
I'd just like to suggest another way.
const user = await this.usersRepository
.findOne(
{
where: { id: In([1, 2, 3]) }
});

remove item from a list by specific index

I have this table:
local ls = {
["foo"] = {1, 2, 3, 4, 5},
["bar"] = {5, 4, 3, 2, 1}
}
I want to remove "foo" from list.
I tried this:
table.remove(ls, "foo")
but returns a error: "Only numbers"
Okay, but I can't input a number. This list isn't static, in my code a lot of indexes will be inserted in this list.
The question is, is there other way to do this or other function that fit my problem?
table.remove only works for a sequence. In your code, the table ls isn't one.
To remove an entry from a table, just assign the value of specific key to nil:
ls.foo = nil

Resources