#firebase #flutter
#firebase #flutter
Вопрос:
Я получаю score
и topicTotal
из состояния и печатаю их, однако я хочу обновить Firebase с topicTotal
помощью извлечения topicTotal
report
. Каждый раз, когда я пытаюсь получить доступ topicTotal
report
, я попадаю null
в Firebase или The getter 'topicTotal' was called on null
.
Как я могу получить доступ topicTotal
к report
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();
}
Модель
class Report {
...
int topicTotal;
Report({ this.uid, this.topics, this.email, this.total, this.topicTotal, this.level, this.id, this.title, this.description, this.img });
factory Report.fromMap(Map data) {
return Report(
uid: data['uid'],
email: data['email'],
total: data['total'] ?? 0,
topics: data['topics'] ?? {},
topicTotal: data['topicTotal'] ?? 34,
level: data['level'] ?? 383,
id: data['id'] ?? '',
title: data['title'] ?? '',
description: data['description'] ?? '',
img: data['img'] ?? 'default.png',
);
}
}
Здесь я могу получить доступ 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: () {
_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
}),
);
}
Комментарии:
1. Вы уверены, что ваш
topicTotal
файл записан в базу данных Firestore? Можете ли вы увидеть его в консоли Firebase для этого документа? имеет ли поле то же имя или нетtopic_total
?2. @MichelFeinstein Спасибо за ваш комментарий.
topicTotal
появляется в базе данных Firestore, и имя точно такое же.3. Вы вызываете все свои настройки, поэтому, возможно, вы обновляете экран, когда одно из ваших значений установлено, но еще не установлено?
notifyListeners
topiTotal
4. @MichelFeinstein Спасибо, я попробовал несколько изменений здесь, но ничего не сработало.
5. Попробуйте создать минимально воспроизводимое приложение