I’m using flutter and I am trying to get a value from shared_preferences for firebase.
i checked my cord doesn’t work and uid was " Instance of ‘Future’ ".
the solution i found was using setState but i can’t use that because it’s not in Widget build.
in this situation, how can i get value(uid) instead of Instance of ‘Future’
class MyItemProvider with ChangeNotifier {
String uid = '';
Future<void> getUid() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
uid = await prefs.getString('uid')!;
}
late CollectionReference itemsReference;
List<Item> items = [];
List<Item> find_result = [];
MyItemProvider({reference}) {
getUid();
itemsReference = reference ?? FirebaseFirestore.instance.collection(uid);
}
...
...
...
i try to use setState
i checked ‘print(uid);’ was Instance of ‘Future’
3
Answers
You have to do following things
You need to await your Futute function "getUid" since it is a type of Future where it is being called.
But you can further your knowledge in dart asynchronous programming, read this
You can use then() method since it returns the value from Future