我试图在共享首选项中保存一些值,并在需要时但在我致电
时检索它String username = SharedPrefUtils.readPrefStr('name');
它获取字符串的值并得到错误
import 'package:shared_preferences/shared_preferences.dart';
class SharedPrefUtils {
static saveStr(String key, String message) async {
final SharedPreferences pref = await SharedPreferences.getInstance();
pref.setString(key, message);
}
static readPrefStr(String key) async {
final SharedPreferences pref = await SharedPreferences.getInstance();
return pref.getString(key);
}
}
节省价值
SharedPrefUtils.saveStr("name", _username);
但我收到此错误!
flutter: Another exception was thrown: type 'Future<dynamic>' is not a subtype of type 'String'
答案 0 :(得分:1)
您必须打字。这可能有效。
Future<String> readPrefStr() async {
final SharedPreferences pref = await SharedPreferences.getInstance();
String value= pref.getString(key);
return value;
}