I will try to answer the question in a general way and indicating what is most important, even if it does not give a specific solution to the problem, after all the resource is being used in a wrong way and the question has no information of what would be the way it would like to do, because it seems that learned that there is the mechanism (map()
) and now he wants to apply it to some problem he doesn’t know what it is. In the middle of what I was writing came a comment showing that the original problem was another.
For the AP (which I know you’ve asked about C#) and some people I’ll compare the map()
with a Select()
LINQ, that is, it selects/maps an object taking data from another object. Taking data like this helps little, it is expected to at least do some manipulation operation of the members of the object when it maps to another object.
I don’t know Dart in depth, but I think it’s worth the same as has already said several times about the abuse of ToList()
in LINQ. People use it more than they should, He is a materializer of the created list, because the previous queries do not generate a list, only generate the algorithm that will produce a way to access manipulated data. If Dart didn’t operate that way then it would always generate objects directly and that would be very inefficient to the point that I would say to throw this thing away on time.
It’s quite simple to use the toList()
, Do you need a list now? Use it if you don’t need to. People create lists without need, often because it just follows the cake recipe. If you only need to access manipulated data that came from another data collection, you cannot create a list. I believe that this comes from the same mistake that makes people create variables without need, they don’t understand what they’re doing there and they think that if you don’t create the variable something will go wrong, she thinks you need to have a name indicating that you have an object. So the person thinks that to access the manipulated data has to create a list. Just create a list if what you want now is exactly a list, no more, no less.
I even question the existence of forEach()
in the language. Do you know why you have this? Why language does not have an imperative abstract construction that traverses a data collection, or you use a for
rough, or use that method. His problem is that it encourages people to use the functional way in an imperative language, and some constructions can be confusing for those who are not used to the functional way. Variable scope can be a problem, just as deviating execution is not possible equal to the complicated imperative way certain situations. There’s a reason LINQ doesn’t have ForEach()
, he forms clever.
So this code makes sense (it gets the total values of the stock, note that I have not created any list, I just took the data from the existing object list):
import 'dart:io';
class Produto {
String nome;
double quantidade;
double valor;
}
main() {
var produtos = List<Produto>();
var p = Produto();
p.nome = 'Frango';
p.quantidade = 2.5;
p.valor = 10.0;
produtos.add(p);
p = Produto();
p.nome = 'Telha';
p.quantidade = 1.5;
p.valor = 20.0;
produtos.add(p);
var totais = produtos.map((Produto p) => p.valor * p.quantidade);
totais.forEach((p) => print(p));
}
If Dart is efficient it has almost the same cost (but not the same because it has the cost of abstraction, it just won’t change the algorithm complexity) to make this code:
import 'dart:io';
class Produto {
String nome;
double quantidade;
double valor;
}
main() {
var produtos = List<Produto>();
var p = Produto();
p.nome = 'Frango';
p.quantidade = 2.5;
p.valor = 10.0;
produtos.add(p);
p = Produto();
p.nome = 'Telha';
p.quantidade = 1.5;
p.valor = 20.0;
produtos.add(p);
for (var i = 0; i < produtos.length; i++) print(produtos[i].valor * produtos[i].quantidade);
}
Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.
I always have to talk about the use of double
in monetary value because someone will read this and think it is right to use it. I can’t help but quote the builder because one might think this example is good (it is simplified only to use the mechanism, in that context it is acceptable to do so).
The method
map
will turn something into something else. In this case, you are mapping the products from a list into nothing, printing halfway (since the return ofprint
isvoid
and you do not specify return)– Jefferson Quesado
It doesn’t help that we don’t have class definition
Produto
. It can be easy to create but it helps if we have it, the link posted has nothing to do with it. It seems that the question has a solution looking for a problem, IE, saw such amap()
And now you want to use it, it’s not usually good. Also because you’re going to abuse it by learning like this. The map alone is good for nothing. You know C#, right? LINQ, right? Themap()
is theSelect()
of LINQ, it alone serves for something useful? It even serves, but almost always a mistake to use it. LINQ does not have aforEach()
You know why? Dart has, you know why?– Maniero
@Maniero added, had forgotten to add.
– rubStackOverflow
That’s the example I saw
children: widget.products.map((Product product) {
 return ShoppingListItem(
 product: product,
 inCart: _shoppingCart.contains(product),
 onCartChanged: _handleCartChanged,
 );
 }).toList(),
I was in doubt why not foreach()?– rubStackOverflow