【发布时间】:2018-11-01 15:34:40
【问题描述】:
我们第一次打开设置高分为0,在颤振中 它将检查分数并更新其值。 我也使用了共享首选项,它不起作用。
import './question.dart';
import 'package:shared_preferences/shared_preferences.dart';
class Quiz {
List<Question> _questions;
int _currentQuestionIndex = -1;
int _score = 0;
int _highscore;
Future checkFirstSeen() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
bool _seen = (prefs.getBool('seen') ?? false);
if (_seen) {
_highscore=_highscore;
} else {
prefs.setBool('seen', true);
_highscore=0;
}
}
Quiz(this._questions) {
_questions.shuffle();
}
List<Question> get questions => _questions;
int get length => _questions.length;
int get questionNumber => _currentQuestionIndex+1;
int get score => _score;
int get highscore => _highscore;
Question get nextQuestion {
_currentQuestionIndex++;
if (_currentQuestionIndex >= length) return null;
return _questions[_currentQuestionIndex];
}
void answer(bool isCorrect) {
if (isCorrect) _score++;
}
//added fun for highscore
void check() {
if (_score > _highscore) {
_highscore = _score;
} else {
_highscore = _highscore;
}
}
}
这返回我总是得分和高分相同的值(数字)。告诉我解决方案
【问题讨论】:
-
_highscore 没有初始化,所以如果你在第一次安装后重新运行你的应用程序,它是空的。
-
是的,当我运行应用程序时,它显示高分和分数相同。
-
@PrakashKing 如果它确实解决了您的问题,请接受答案。
标签: android flutter google-play-games flutter-dependencies