颤抖:引发了另一个异常:类型'Future <dynamic>'不是类型'String'的子类型

时间:2020-07-28 09:25:16

标签: android ios flutter dart flutter-layout

我试图在共享首选项中保存一些值,并在需要时但在我致电

时检索它
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'

1 个答案:

答案 0 :(得分:1)

您必须打字。这可能有效。

 Future<String> readPrefStr() async {
   final SharedPreferences pref = await SharedPreferences.getInstance();
    String value= pref.getString(key);
    return value;
  }