首页 > 解决方案 > 在颤动中动态制作 TabBarView

问题描述

情况:

我有两个列表,tabsText并且mainListAllPlantDetailsList1.
选项卡文本

  List<String> tabsText = ["Top","Outdoor","Indoor", "Seeds", "Flowers"];

mainListAllPlantDetailsList1

List<PlantDetails> mainListAllPlantDetailsList1 = [
    PlantDetails(
      indexNumber: 0,
      category: "flower",
      plantName: "Lamb's Ear",
      price: 147,
      description: "This is the Lamb's Ear flower"
    ),
    PlantDetails(
      indexNumber: 1,
      category: "seeds",
      plantName: "Lotus",
      price: 120,
      description: "This is the lotus seeds"
    ),
    PlantDetails(
      indexNumber: 2,
      category: "indoor",
      plantName: "Hughusi Yoon",
      price: 147,
      description: "This is the Hughusi Yoon indoor"
    ),
    PlantDetails(
      indexNumber: 3,
      category: "outdoor",
      plantName: "lily",
      price: 120,
      description: "This is the lily outdoor"
    ),
  ];

细节:

我在tabsText列表中使用 for 循环制作了 TabBar。现在我再次TabBarView使用 for 循环,我尝试过这样

tabViewerMaker(){
  List<Widget> tabBarView = List();
  for(var i = 0; i<tabsText.length;i++){
    for( var j =0; j<mainListAllPlantDetailsList1.length;j++){
      if(tabsText[i] == mainListAllPlantDetailsList1[j].ca){
        tabBarView.add(
        Text("We found category"+tabsText[i])
        );
      }
      else{
        continue;
      }
    }
  }
}

在这里,我检查了tabsTextand mainListAllPlantDetailsList1。如果特定索引的 StringtabsText等于 then 的特定索引,mainListAllPlantDetailsList1则生成一个小部件,
Text("We found category"+tabsText[i]) 否则继续。而且我已经列出了类型小部件并添加了所有文本小部件,但是在调用函数时tabViewerMaker 为什么会出错?

══╡ EXCEPTION CAUGHT BY WIDGETS LIBRARY ╞═══════════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building Container(bg: BoxDecoration(color:
Color(0xffffffff)), constraints: BoxConstraints(w=Infinity, h=317.0)):
The getter 'key' was called on null.

问题:

如何使用 for 循环实现所需的功能或随意提出任何建议?

标签: listfluttertabstabbar

解决方案


推荐阅读