标记需要构建StreamProvider小部件显示的错误



我得到了以下代码的错误消息,我试图在buildContext上使用StreamProvider,我已经在initState进程中添加了'listen: false'。知道怎么改代码吗?问题到底出在哪里?

@override
void initState() {
final gameProvider = Provider.of<GameProvider>(context,listen :false);
gameProvider.changeGameId = widget.game.gameId;
super.initState();
}
@override
Widget build(BuildContext context) {
final gameProvider = Provider.of<GameProvider>(context);
//    gameProvider.changeGameId = widget.game.gameId;
if (gameProvider == null) return Container();
return Scaffold(
appBar: AppBar(
title: Text('OnLine BlackJack'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Stack(
children: [
Positioned(
top: 420,
left: 1,
height: 180,
width: 99, //+ gameProvider.playerHandJson.length * 30,
//duration: Duration(seconds: 1),
child: Container(
child: gameProvider.playerHandJson != null
? HandJsonDecode(handJson: gameProvider.playerHandJson)
: Container(),
)),

如果你能出示你的GameProvider,将有助于我们更好地理解问题。我也认为你正在调用一个函数changeGameId作为一个变量。

gameProvider.changeGameId = widget.game.gameId;
gameProvider.changeGameId(widget.game.gameId);

我想你在你的提供商

中有类似的内容
class GameProvide extend ChangeNotifier{
final _gameId;
void function changeGameId(String id){
_gameId = id;
notifyListeners();
}
}

我已经通过使用StreamBuilder解决了这个问题,但不是为什么changeGameId会导致错误。我确实在GameProvider类中编码了changeGameId函数。

Widget build(BuildContext context) {

return Scaffold(
appBar: AppBar(
title: Text('OnLine BlackJack'),
),
body: Padding(
padding: const EdgeInsets.all(8.0),
child: Stack(
children: [
StreamBuilder<Game>(
stream: firestoreService.joinedGame(widget.game.gameId),
builder: (context, snapshot) {
var game = snapshot.data;
if (game != null) {
return Stack(children: [
Positioned(
top: 1,
left: 1,
width: (game.playerHandJson.length) * 30.0 + 69,
height: 180,
child: Container(
child: HandJsonDecode(
handJson: game.playerHandJson,
))),

相关内容

最新更新