- Edited
Best score’u ayrı bir yerde tutmak istiyorum. Shared preferences kullanamayı denedim ancak hata alıyorum.
import 'package:shared_preferences/shared_preferences.dart';
int best;
setBest(int n) async {
SharedPreferences prefs = await SharedPreferences.getInstance();
prefs.setInt('best', n);
}
getBest() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
int intValue = prefs.getInt('best');
if (intValue == null) {
return 0;
}
return intValue;
}`
Bu dart dosyasını başka bir dart dosyasında import .... as x olarak cagırıyorum.
O sayfanın kodu.
`int best = x.getBest();
@override
void initState() {
super.initState();
if (score > best) {
best = score;
x.setBest(score);
}
}
Buda hata mesajı.
Exception caught by widgets library
The following _TypeError was thrown building Builder:
type 'Future<dynamic>' is not a subtype of type 'int'
The relevant error-causing widget was MaterialApp lib\main.dart:9
When the exception was thrown, this was the stack
#0 new _QuizResultState package:quiz_game/pages/quiz_result.dart:16
#1 QuizResult.createState package:quiz_game/pages/quiz_result.dart:12
#2 new StatefulElement package:flutter/…/widgets/framework.dart:4591
#3 StatefulWidget.createElement package:flutter/…/widgets/framework.dart:894
Normal element mounting (115 frames)