Marzook
Marzook

Reputation: 391

flutter: Another exception was thrown: type 'Future<dynamic>' is not a subtype of type 'String'

I tried to save some value in shared preference and retrieve it whenever I need it but when i call the

String username = SharedPrefUtils.readPrefStr('name');

it dosent get value to the string and getting the error

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);
}

}

saving value

 SharedPrefUtils.saveStr("name", _username);

but I am getting this error!

flutter: Another exception was thrown: type 'Future<dynamic>' is not a subtype of type 'String'

Upvotes: 0

Views: 297

Answers (1)

Rajshekhar
Rajshekhar

Reputation: 614

You have to typecast. This Might work.

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

Upvotes: 1

Related Questions