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>
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 .