【发布时间】:2020-09-03 09:20:25
【问题描述】:
如何创建等于列表长度的选项卡,并且它们的名称应该等于列表中项目的名称,并且我正在从 firebase 实时数据库中获取列表。
这是我的代码:
class _ItemDetailsDemoState extends State<ItemDetailsDemo> with SingleTickerProviderStateMixin {
@override
void initState() {
getSubCategories();
super.initState();
}
List<SubCategoryLoader> subCategoryLoaderList = List();
Future<void> getSubCategories() async {
await FirebaseDatabase.instance.reference().child("SubCategoryNames").once()
.then((DataSnapshot snapshot) {
var key = snapshot.value.keys;
;
for (var i in key) {
SubCategoryLoader subCategoryLoader = new SubCategoryLoader(
snapshot.value[i]['Name']
);
subCategoryLoaderList.add(subCategoryLoader);
}
for (int j = 0; j < subCategoryLoaderList.length; j++) {
print(subCategoryLoaderList[j].Name);
}
if (mounted) {
setState(() {
print(subCategoryLoaderList.length);
});
}
});
}
@override
Widget build(BuildContext context) {
return FutureBuilder(
future: FirebaseDatabase.instance.reference().child("SubCategoryNames").once(),
builder: (context,snapshot){
if(snapshot.hasData){
if(snapshot.data!=null)
{
return DefaultTabController(
length: subCategoryLoaderList.length, // I made this dynamic but this is throwing an error "controller's length property does not match with number of tabs, this is because my Tab is static which is 2 how can I make it dynamic.
child: Scaffold(
appBar: AppBar(
bottom: TabBar(
tabs: [
Tab(icon: Icon(Icons.looks_one), text: "List1"), //How can i make this dynamic and text:"List1" must be the name of list items
Tab(icon: Icon(Icons.looks_two), text: "List2"),
],
),
),
body: TabBarView(
children: [
_buildList(key: "key1", string: "a"),
_buildList(key: "key2", string: "List2: "),
],
),
));
}else{
return Loader();
}
}else{
return Loader();
}
},
);
}
Widget _buildList({String key, String string}) {
return ListView.builder(
shrinkWrap: true,
scrollDirection: Axis.vertical,
itemCount: subCategoryLoaderList.length,
itemBuilder: (context, index1){
return Container(
child: Text(subCategoryLoaderList[index1].Name+string),
);
},
);
}
}
我还希望 TabBarView 也是动态的,以便相应地呈现项目。我需要获取属于该子类别的所有数据。
【问题讨论】:
标签: flutter firebase-realtime-database tabs tabbarcontroller