You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Flutter中ChangeNotifier内Player分数更新不持久问题求助

问答游戏分数持久化问题排查与解决

问题描述

开发问答游戏时,通过AnswerCard的GestureDetector调用addResponse/subResponse更新Player分数后,切换到下一题或结果页面,分数会恢复为默认值0。项目中GameController继承自ChangeNotifier,包含Game类型属性,Game拥有List<Player>类型的队伍列表。

核心原因

问题出在_HomeState的build方法中:每次build执行时,都会新建一个全新的Game实例,并调用gameController.setGame(game)覆盖掉之前已经修改过分数的Game对象。而build方法会在多种场景下触发(比如导航返回、其他组件状态变化导致页面重绘),这就导致之前的分数数据被频繁重置。

修复步骤

  1. 将Game实例的初始化和setGame调用移到initState方法中,确保只在页面初始化时执行一次,避免重复重置数据。
  2. 确保GameController的作用域正确(比如在应用根节点提供),避免控制器被意外重建。

修复后的代码示例

修改后的_HomeState代码

class _HomeState extends State<Home> {
  @override
  void initState() {
    super.initState();
    // 只在页面初始化时创建Game实例并设置到控制器
    var game = new Game(
      teamsList: [
        Player(name: "Aerobotix", score: 0),
        Player(name: "IEEE", score: 0),
        Player(name: "Theatro", score: 0),
      ],
      nbRounds: 2,
      questionsList: [
        new Question(answersList: [
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
        ], question: "question1", timer: 30),
        new Question(answersList: [
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
        ], question: "question2", timer: 30),
        new Question(answersList: [
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
        ], question: "question3", timer: 30),
        new Question(answersList: [
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
        ], question: "question4", timer: 30),
        new Question(answersList: [
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
        ], question: "question5", timer: 30),
        new Question(answersList: [
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
          {"answer": "ijeba", "score": 10},
        ], question: "question6", timer: 30),
      ],
    );
    final GameController gameController = Provider.of<GameController>(context, listen: false);
    gameController.setGame(game);
  }

  @override
  Widget build(BuildContext context) {
    print("call build in home screen");
    return Scaffold(
      body: ElevatedButton.icon(
        icon: Icon(Icons.play_arrow),
        label: Text("开始游戏"),
        onPressed: () {
          Navigator.push(
            context,
            MaterialPageRoute(
              builder: (context) {
                return StartGame();
              },
            ),
          );
        },
      ),
    );
  }
}

额外注意事项

  • 调用Provider.of时添加listen: false,避免在initState中触发不必要的监听。
  • 如果需要支持重新开始游戏,应该添加一个专门的"重置游戏"方法,而不是依赖页面重建来重置数据。
  • 确保Player和Game类的字段是可变的(当前代码已满足),如果后续改为不可变类,需要通过创建新实例并更新控制器的方式来修改数据,同时调用notifyListeners()。

内容的提问来源于stack exchange,提问作者zied kanoun

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.25 20:20:05