I need some help here i am trying to this getdetails.getuserid method in the another class but i am getting error like this type 'Future<dynamic>' is not a subtype of type 'String'

class getdetails {
  Future<Null> getuserid() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    String userid = pref.getString('userid');
    return userid;
  }
}

I am getting this error Closure: () => dynamic from Function 'getuserid': static.() in dart if i try to call like below

String getuserid() async {
    SharedPreferences pref = await SharedPreferences.getInstance();
    String userid = pref.getString('userid');
    return userid;
  }


Solution 1: Rémi Rousselet

You need to define the return type of your function to Future<String>

Future<String> getuserid() async {
 ....
}


Solution 2: rannes

remove null and place String after Future... Future


Solution 3: Aman Malhotra

You have got quiet a few errors there .

  1. In the first code sample you were returning a null future while your code you were returning a string . that dosen't make any sense.
  2. In your second code sample you are returning just a string while your function is an async function so its another error that might even be shown to u on whichever IDE you are working with . something like -> you need to return a future . So it should be Future as mentioned in the previous answer.

Future<String>
  1. Create a String var just say

    String userId = "";

inside your class getdetails. then inside your function getuserid() do this supposing that you already have some value in userid key that you are using for shared preferences.

getuserid() async{
SharedPreferences pref = await SharedPreferences.getInstance(); 
String u = pref.getString('userid'); 
userId = u ;
}

now create another function in same class just to make things simple and clear .

String userdetails(){
return userId;
}

now in the class/widget(stateful/stateless) where you want to get the userId do this :-

create an object of that class like this

getdetails g ; 

now call the functions created like this ..

g.getuserid().then((){
String ID = g.userdetails();
});

What i am doing here is first create a function that gets your value and stores it into a local variable then call another function to get the value of that local variable when the value have been retrieved from the shared prefs class and stored into that local variable using the same object i.e why i used .then((){}) function I hope it helps .