Hello, I am logging in with firebase but with the user's uid I want to consult an api-rest and if it brings data it means that it is registered and it shows a screen, otherwise it shows another screen
Future<void> _authStateChanged(AuthUser? user) async {
try {
final usuario = 'uWb0PKBbhBZVirLh0bllk2ncDRp1hhhh';
final dataSource = RestDataSourceMySql();
final result = await dataSource.getName(usuario);
if (user != null && result != null) {
print('trajo datos');
print(result);
emit(AuthSignedWithMysql(user));
} else if (user != null && result == null) {
emit(AuthNoRegister(user));
} else if (user == null) {
emit(AuthSignedOut());
}
} on Exception catch (_) {
print("Se cayo");
}
If it brings data it shows me the new screen without problems, but if it brings the uid and in the rest api it doesn't bring anything I get this message "RangeError (RangeError (index): Invalid value: Valid value range is empty: 0)"
Preamble. A little advice.
The truth is that you are giving very little information regarding the error. Also, you still, at the time of writing this answer, have questions without marking a correct answer. Remember that by doing so, you encourage good use of the site. If you feel that there is no correct answer to your question, perhaps you should add more information to it.
You should expose the part of the code related to the problem. There's only one method there, which doesn't reveal where/how you want to call the method from.
The code-related parts in this case could be the Notepad you're using to call the method, but only the Notepad and that method.
Answer I think you are using
Flutter_Bloc
, which is not the same as the patternBloC
, which can lead to confusion, since the pattern can be implemented with any state manager.Normally, in these cases, what is done is that the Bloc receives the repository as a dependency. Something like that:
And now, inside the block, you could call that repository to query the user's information, thanks to the UID that comes to you as a parameter of the method: