【发布时间】:2021-10-29 17:09:01
【问题描述】:
我试图从map 中获取一些随机的多个值作为列表,代码中有一些解释 //,任何逻辑如果 Function 或喜欢我的逻辑,在此先感谢
class Test{
String url;
int id;
String name;
Test({required this.url , required this.id , required this.name});
}
class Fake{
static List<Test> getSomeValues = [
Test(id: 1 , url : "test/test" , name : "1Name"),
Test(id: 2 , url : "test/test" , name : "2Name"),
Test(id: 3 , url : "test/test" , name : "3Name"),
];
}
void widget(Test t){
print(t.id);
}
void main(){
print(Fake.getSomeValues.asMap().entries.where((f){
// here just I can apply condition on each Value can't get random
return f.value.id == 1 ;
}).map((f){
// I don't want not needy value arrive here .
// after I want to use each instance of Test .
return widget(f.value);
}).toList());
}
例如输出:
void getRandom(List<test> t , int count){
// code
print(t.id)
}
getRandom(test , 2),
输出:
3,1
【问题讨论】:
-
你想用map获取随机数吗?
-
如果你的逻辑返回列表很好:)
-
@Mo7ammed7amad 更新了我的答案,请看一下
标签: list flutter dictionary dart