'How to get data from a repository from within a cubit?
i'm using flutter bloc. i want to emit the data from a repo to a state.
how do i get the data from the repo to show in the cubit?
This is my cubit:
void getProfile(userId) async {
try {
final data = await profileRepository.fetchProfile(userId: userId)
as Map<String, dynamic>;
if (data != null) {
final _userId = (data['userId'] ?? '') as String;
final _username = (data['username'] ?? '') as String;
final _avatarUrl = (data['avatar_url'] ?? '') as String;
final _website = (data['website'] ?? '') as String;
print('this is from a cubit');
print('$_userId $_username $_avatarUrl $_website');
}
} catch (_) {
emit(const _Failure());
}
}
this is my repo:
Future fetchProfile({
required String userId,
String? username,
}) async {
try {
final response = await supabase
.from('profiles')
.select()
.eq('id', userId)
.single()
.execute();
final data = response.data as Map<String, dynamic>;
if (data != null) {
_userId = (data['userId'] ?? '') as String;
_username = (data['username'] ?? '') as String;
_avatarUrl = (data['avatar_url'] ?? '') as String;
_website = (data['website'] ?? '') as String;
}
print('fetched profile profile inside repository');
print('$_userId $_username $_avatarUrl $_website');
return;
} catch (e) {
print(e.toString());
}
}
the print statement from the repo works fine. the print statement from the cubit fails.
thanks
Solution 1:[1]
Duh. All I needed to do was change the return statement in fetchProfile
to:
return data;
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | dKen |