参数类型“Future<>”不能分配给参数类型“”
Posted
技术标签:
【中文标题】参数类型“Future<>”不能分配给参数类型“”【英文标题】:The argument type 'Future<>' can't be assigned to the parameter type '' 【发布时间】:2021-01-05 23:46:21 【问题描述】:我正在尝试构建一个 GridView 列表。当我创建一个简单的“玩家”列表时,这一切都有效,但现在我尝试使用 sqflite 持久化数据,但出现错误:
错误:无法将参数类型“Future”分配给参数类型“int”
这是我的列表构建器小部件:
class PlayerList extends StatelessWidget
@override
Widget build(BuildContext context)
return Consumer<PlayerData>(
builder: (context, playerData, child)
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 20,
crossAxisSpacing: 20,
),
itemCount: playerData.playerCount,
itemBuilder: (BuildContext context, int index)
final player = playerData.players[index];
return RawMaterialButton(
fillColor: Color(0x991c5597),
elevation: 20,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.tealAccent,
radius: 30,
child: Icon(
Icons.star,
color: Color(0xFF16377a),
size: 30,
),
),
Text(player.name, style: smallTitleText),
],
),
),
onPressed: ()
playerData.makeActive(player.name);
playerData.printList();
Navigator.pushNamed(context, PlayScreen.id);
);
);
,
);
这就是我获取列表计数的方式:
Future<int> get playerCount async
final Database db = await getDBConnector();
final List<Map<String, dynamic>> maps = await db.query('players');
if (maps.length != null)
return maps.length;
else return 0;
【问题讨论】:
你到底在哪里得到错误? 我想添加日志,以便我们知道问题到底出在哪里。 【参考方案1】:出现错误是因为 playerCount 没有返回 int,而是返回了 Future。首先,您必须将您的类更改为有状态小部件并加载数据,如下所示
class PlayerList extends StatefulWidget
@override
_PlayerListState createState() => _PlayerListState();
class _PlayerListState extends State<PlayerList>
int count;
@override
void initState()
super.initState();
loadPlayerCount();
@override
Widget build(BuildContext context)
return Consumer<PlayerData>(
builder: (context, playerData, child)
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 20,
crossAxisSpacing: 20,
),
itemCount: count??0,
itemBuilder: (BuildContext context, int index)
final player = playerData.players[index];
return RawMaterialButton(
fillColor: Color(0x991c5597),
elevation: 20,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.tealAccent,
radius: 30,
child: Icon(
Icons.star,
color: Color(0xFF16377a),
size: 30,
),
),
Text(player.name, style: smallTitleText),
],
),
),
onPressed: ()
playerData.makeActive(player.name);
playerData.printList();
Navigator.pushNamed(context, PlayScreen.id);
);
);
,
);
void loadPlayerCount() async
final Database db = await getDBConnector();
final List<Map<String, dynamic>> maps = await db.query('players');
if (maps.length != null)
setState(()
this.count = maps.length;
);
else
setState(()
this.count = 0;
);
【讨论】:
【参考方案2】:用FutureBuilder包裹你的GridView
类似的东西
return FutureBuilder(
future: playerData.playerCount,
builder : (context,snapshot)
if(!(snapshot.hasData))
return Container();
else
return GridView.builder(
gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
crossAxisCount: 2,
mainAxisSpacing: 20,
crossAxisSpacing: 20,
),
itemCount: snapshot.data,
itemBuilder: (BuildContext context, int index)
final player = playerData.players[index];
return RawMaterialButton(
fillColor: Color(0x991c5597),
elevation: 20,
shape: RoundedRectangleBorder(
borderRadius: BorderRadius.circular(10),
),
child: Padding(
padding: const EdgeInsets.all(10.0),
child: Column(
crossAxisAlignment: CrossAxisAlignment.center,
mainAxisAlignment: MainAxisAlignment.spaceEvenly,
children: <Widget>[
CircleAvatar(
backgroundColor: Colors.tealAccent,
radius: 30,
child: Icon(
Icons.star,
color: Color(0xFF16377a),
size: 30,
),
),
Text(player.name, style: smallTitleText),
],
),
),
onPressed: ()
playerData.makeActive(player.name);
playerData.printList();
Navigator.pushNamed(context, PlayScreen.id);
);
);
,
);
);
【讨论】:
以上是关于参数类型“Future<>”不能分配给参数类型“”的主要内容,如果未能解决你的问题,请参考以下文章
获取错误“未知”类型的参数不能分配给“错误”类型的参数 |空值'
参数类型“对象?”不能分配给参数类型'String'最新版本
参数类型“Type”不能分配给参数类型“FirebaseUser”