Flutter 来自模型提供者的颤振搜索

Flutter 来自模型提供者的颤振搜索,flutter,dart,Flutter,Dart,几天后,它不起作用了,我最初将模型上的数据合并在一起 示例:三星有很多数据,我将其组合成一个阵列,我想问一下如何根据型号的品牌名称进行搜索 @override Widget build(BuildContext context) { remoteModelId.clear(); isLoading = true; final products = Provider.of<List<Brands>>(context); return Sc

几天后,它不起作用了,我最初将模型上的数据合并在一起 示例:三星有很多数据,我将其组合成一个阵列,我想问一下如何根据型号的品牌名称进行搜索

@override
  Widget build(BuildContext context) {
    remoteModelId.clear();
    isLoading = true;
    final products = Provider.of<List<Brands>>(context);
    return Scaffold(
        ...
        body: products != null
            ? ListView.separated(
                itemCount: products.length,
                itemBuilder: (context, index) {
                  
                  var lol = [];
                  var idModel = [];
                  var sublist = [].join();
                  var countList =[];
                  //var allList =[];
                 
                  //var subLol = lol.indexOf(lol);
                  for(var ok in products){
                    lol.add(ok.brandName);
                    idModel.add(ok.ids);
                     countList.add(lol);
                    if(lol.contains(lol)){
                      sublist.compareTo(lol[index]);
                      break;
                    }
                    
                  }
                  distinctIds = lol.toSet().toList();
                  hasilakhir = Set.of(distinctIds).toList();
                  newDataList = List.from(distinctIds);
                  templist.add(hasilakhir);
                  
                  final myMap = Map();
                  
                  lol.forEach((element) { 
                    if(!myMap.containsKey(element)){
                      myMap[element] = 1;
                      return false;
                    }else{
                      myMap[element] += 1;
                      return false;
                    }
                  });
                  //newDataList = newDataList.map((brand)=>brand.toLowerCase()).toList();                
                  return ListTile(
                    title: Text(hasilakhir[index]),
@覆盖
小部件构建(构建上下文){
remoteModelId.clear();
isLoading=true;
最终产品=供应商(上下文);
返回脚手架(
...
正文:产品!=null
?ListView.com(
itemCount:products.length,
itemBuilder:(上下文,索引){
var-lol=[];
变量idModel=[];
var sublist=[].join();
var countList=[];
//var allList=[];
//var subLol=lol.indexOf(lol);
对于(产品中的var ok){
lol.add(ok.brandName);
添加(ok.ids);
countList.add(lol);
如果(lol.contains(lol)){
子列表比较(lol[索引]);
打破
}
}
differentids=lol.toSet().toList();
hasilakhir=一组(区别).toList();
newDataList=List.from(区别);
圣殿骑士.add(hasilakhir);
final myMap=Map();
lol.forEach((元素){
如果(!myMap.containsKey(元素)){
myMap[元素]=1;
返回false;
}否则{
myMap[元素]+=1;
返回false;
}
});
//newDataList=newDataList.map((brand)=>brand.toLowerCase()).toList();
返回列表块(
标题:文本(hasilakhir[索引]),

谢谢

我不确定您需要什么,但下面是一些示例代码:

  // Get products with a specific brandName
  print(
      'Number of Samsung=${products.where((p) => p.brandName == 'Samsung').length}');

  // Count products by brandName - like your code
  final map = Map();
  products.forEach((product) {
    if (!map.containsKey(product.brandName)) {
      map[product.brandName] = 1;
    } else {
      map[product.brandName] += 1;
    }
  });
  print('map=$map');
  
  // Group products by brandName, with the count and list of ids
  final map2 = Map();
  products.forEach((product) {
    if (!map2.containsKey(product.brandName)) {
      map2[product.brandName] = {
        'count': 1,
        'ids': [product.ids]
      };
    } else {
      var current = map2[product.brandName];
      current['count']++;
      current['ids'].add(product.ids);
    }
  });
  print('map2=$map2');
我的测试数据的输出为:

Number of Samsung=3
map={Samsung: 3, Apple: 3}
map2={Samsung: {count: 3, ids: [S1, S2, S3]}, Apple: {count: 3, ids: [iPhone1, iPhone2, iPhone3]}}
我不知道它是否有用,但我已在下面对您的一些代码进行了注释:

  // Get list of distinct brandNames
  var distinctIds = lol.toSet().toList();
  // Get list of distinct brandNames - same as distinctIds
  var hasilakhir = Set.of(distinctIds).toList();
  // Copy list distinctIds
  var newDataList = List.from(distinctIds);
  List templist = [];
  // Make a list with one element which is the list of brandNames
  templist.add(hasilakhir);

很抱歉,我不明白你在问什么。你想“搜索”代码中的哪个列表,以及对结果做什么?如果(lol.contains(lol))测试将永远不会成功:)对于难以理解的解释,我很抱歉,我的意思是在itemBuilder到达listTile后忽略代码,我所问的是基于模型名称搜索数据的函数BrandThank感谢您的关注,我会尝试代码解决方案