我想使用從 Api 獲得的扇區串列(扇區模型)顯示我的選項卡,如下所示:
SizedBox(
child: DefaultTabController(
initialIndex: 1,
length: sectorsProvider.sectorList.length,
child: DecoratedBox(
decoration: BoxDecoration(
//This is for background color
color: Colors.white.withOpacity(0.0),
//This is for bottom border that is needed
border: Border(
bottom: BorderSide(color: Colors.white, width: 0.4),
),
),
child: ListView(
shrinkWrap: true,
children: [
TabBar(
isScrollable: true,
indicatorColor: Colors.black,
controller: _tabController,
indicatorSize: TabBarIndicatorSize.label,
indicatorWeight: 3.0,
labelStyle: TextStyle(
fontSize: 22.0,
),
// unselectedLabelStyle: TextStyle(fontSize: 10.0, color: Colors.black26,),
tabs: (sectorsProvider.sectorList as List<Sector>).map((index) => Tab(text: sectorsProvider.sectorList[index as int].name)).toList(),
),
],
),
),
),
),
我有以下錯誤:型別'Sector'不是型別轉換中'int'型別的子型別..
感謝幫助!
uj5u.com熱心網友回復:
index as int
您的編譯器告訴您這不是一個顯而易見的事實,不int應該讓您三思??而后行。
index在這種情況下是型別Sector,因為您提供給的回呼map采用Iterable. 在你的情況下,因為它是 aList<Sector>那是 a Sector。
所以sectorsProvider.sectorList[index as int].name不需要,你只需要index.name. 盡管重命名變數可能會更好:
tabs: (sectorsProvider.sectorList as List<Sector>).map((sector) => Tab(text: sector.name)).toList()
您需要投射的事實sectorsProvider.sectorList看起來List<Sector>也很可疑。你不應該投射任何東西。每當您這樣做時,請尋找可以使演員表多余而不是演員表的改進。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/477914.html
