我们第一次打开将高分设为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;
}
}
}
这总是返回总分和高分相同的值(数字)。告诉我解决方案
答案 0 :(得分:0)
如果我正确理解了您要执行的操作,则需要按以下方式编辑c:\SpecRun\SpecRun.exe run Default.srprofile /baseFolder:C:\testBase /filter:"@MobileGalaxy" /log:specrun.log /outputfolder:output /report:MobileGalaxy.html
和Quiz.checkFirstSeen()
方法:
Quiz.check()
在您发布的代码中,实际上不需要Future<void> checkFirstSeen() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
_highscore = prefs.getInt('highScore') ?? 0;
}
Future<void> check() async {
final SharedPreferences prefs = await SharedPreferences.getInstance();
if (_score > _highscore) {
_highscore = _score;
await prefs.setInt('highScore', _highscore);
}
}
共享变量,因此我将其删除。