用最终分数更新Firebase并显示给用户

用最终分数更新Firebase并显示给用户,firebase,flutter,Firebase,Flutter,我正在从该州获取分数和topicTotal,并将其打印出来,但是,我想通过在报告中检索topicTotal来用topicTotal更新Firebase。每次我试图在报告中访问topicTotal,我都会在Firebase中得到null,或者在null上调用getter“topicTotal” 如何访问报告中的topicTotal,并使用它更新Firebase?另外,如何向用户显示topicTotal set score(Options newValue) { var score = id

我正在从该州获取
分数
topicTotal
,并将其打印出来,但是,我想通过在
报告
中检索
topicTotal
来用
topicTotal
更新Firebase。每次我试图在
报告中访问
topicTotal
,我都会在Firebase中得到
null
,或者
在null上调用getter“topicTotal”

如何访问
报告中的
topicTotal
,并使用它更新Firebase?另外,如何向用户显示
topicTotal

set score(Options newValue) {
    var score = idx += newValue.score; 
    _score = newValue;              
    print(score);
    _score = newValue;  
    notifyListeners();
  }
  
  set topicTotal(Options newValue) {
    var topicTotal = idx;
    _score = newValue;
    this._topicTotal = newValue;
    print(topicTotal);
    notifyListeners();
  }
模型

在这里,我可以访问
total
,但不能访问
topicTotal

 if (report != null)
              Text('${report.total ?? 0}',
                  style: Theme.of(context).textTheme.display3),
            Text('Assessments Completed',
                style: Theme.of(context).textTheme.subhead),
            Spacer(),
            if (report != null)
            Text('your topic score is ${report.topicTotal ?? 0}'),
onPressed:(){
_更新带有主题的SERReport(评估、状态、选项选择);
未来更新SERReportWithTopics(评估、评估状态、选项选择){
state.topicTotal=选择的选项;
返回Global.reportRef.upsert(
({
“总计”:字段值。增量(1),
“主题”:{
“${assessment.topic}”:FieldValue.arrayUnion([
评估。标题,
评估。总主题,
评估、问题、长度、,
]),
},
//“topicTotal”:state.topicTotal=选项已选择
}),
);
}

您确定您的
topicTotal
已记录到Firestore数据库中吗?您能在Firebase控制台上看到该文档吗?该字段是否具有相同的名称或是否为
topic\u total
?@MichelFeinstein感谢您的评论。
topicTotal
确实出现在Firestore数据库中,并且名称正是sa我。你在所有设置器上调用
notifyListeners
,所以当设置了一个值时,你可能正在更新屏幕,但
topiTotal
尚未设置?@MichelFeinstein谢谢,我在这里尝试了几次更改,但都没有效果。尝试制作一个最小的可复制应用程序
 if (report != null)
              Text('${report.total ?? 0}',
                  style: Theme.of(context).textTheme.display3),
            Text('Assessments Completed',
                style: Theme.of(context).textTheme.subhead),
            Spacer(),
            if (report != null)
            Text('your topic score is ${report.topicTotal ?? 0}'),
 onPressed: () {
                  _updateUserReportWithTopics(assessment, state, optionSelected);

Future<void> _updateUserReportWithTopics(Assessment assessment, AssessmentState state, Options optionSelected) {
  state.topicTotal = optionSelected;
  return Global.reportRef.upsert(
    ({
      'total': FieldValue.increment(1),
      'topics': {
        '${assessment.topic}': FieldValue.arrayUnion([
          assessment.title,
          assessment.topicTotal,
          assessment.questions.length,
        ]),
      },
      
      //'topicTotal': state.topicTotal = optionSelected
    }),
  );
}