type 'Sector' is not a subtype of type 'int' in type cast flutter 问题?
type 'Sector' is not a subtype of type 'int' in type cast flutter problem?
我想使用从 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' 的子类型..
感谢帮助!
index as int
您的编译器告诉您这不是 int
的明显事实应该让您三思而后行。
index
在这种情况下是 Sector
类型,因为您提供给 map
的回调采用 Iterable
的元素。在你的例子中,因为它是一个 List<Sector>
即 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>
的事实看起来也很可疑。你不应该投任何东西。每次执行此操作时,寻找可以使强制转换变得多余的改进,而不是强制转换。
我想使用从 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' 的子类型..
感谢帮助!
index as int
您的编译器告诉您这不是 int
的明显事实应该让您三思而后行。
index
在这种情况下是 Sector
类型,因为您提供给 map
的回调采用 Iterable
的元素。在你的例子中,因为它是一个 List<Sector>
即 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>
的事实看起来也很可疑。你不应该投任何东西。每次执行此操作时,寻找可以使强制转换变得多余的改进,而不是强制转换。