I am getting response data from an api while passing in a response body. I have built my state, event, repository of my request but upon calling the user details event in my home page where I want to access my data, I am getting an error below
W/WindowOnBackDispatcher(10169): OnBackInvokedCallback is not enabled for the application.
W/WindowOnBackDispatcher(10169): Set ‘android:enableOnBackInvokedCallback=”true”‘ in the application manifest.
E/flutter (10169): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: Bad state: add(FetchUserDetails) was called without a registered event handler.
E/flutter (10169): Make sure to register a handler via on((event, emit) {…})
E/flutter (10169): #0 Bloc.add. (package:bloc/src/bloc.dart:91:9)
E/flutter (10169): #1 Bloc.add (package:bloc/src/bloc.dart:97:6)
E/flutter (10169): #2 _DashboardPageState._loadUsernameAndPin (package:mobile_banking/views/dashboard_page.dart:108:39)
E/flutter (10169):
Here is how I am implementing my whole logic.
class UserDetails {
final String username;
final String password;
final String deviceId; // Add required fields
final String ipAddress;
final String appVersion;
final String platform;
UserDetails({
required this.username,
required this.password,
required this.deviceId,
required this.ipAddress,
required this.appVersion,
required this.platform,
});
factory UserDetails.fromJson(Map<String, dynamic> json) {
final data = json['data'];
print(data);
return UserDetails(
username: json['username'] ?? '',
password: json['password'] ?? '',
deviceId: json['deviceId'] ?? '',
ipAddress: json['ipAddress'] ?? '',
appVersion: json['appVersion'] ?? '',
platform: json['platform'] ?? '',
);
}
Map<String, dynamic> toJson() {
return {
'username': username,
'password': password,
'deviceId': deviceId,
'ipAddress': ipAddress,
'appVersion': appVersion,
'platform': platform,
};
}
@override
String toString() {
return 'UserDetails{username: $username, deviceId: $deviceId, ipAddress: $ipAddress, appVersion: $appVersion, platform: $platform}';
}
}
my user details repository
class UserDetailsRepository {
final String endpoint = 'url';
late HttpService httpService;
Future<UserDetails> getUserDetails(UserDetails userDetails) async {
try {
print('Requesting user details with: ${userDetails.toJson()}');
final response = await httpService.request(
method: HttpMethod.post,
url: '$endpoint/api/iBank/UserAuth',
body: userDetails.toJson(),
);
print("Response body: $response");
print("Return response body: ${UserDetails.fromJson(response)}");
return UserDetails.fromJson(response);
} catch (e) {
throw Exception('Failed to get user details');
}
}
}
my state
abstract class UserDetailsState extends Equatable {
@override
List<Object?> get props => [];
}
class UserDetailsInitial extends UserDetailsState {}
class UserDetailsLoading extends UserDetailsState {}
class UserDetailsLoaded extends UserDetailsState {
final Map<String, dynamic> userDetails;
UserDetailsLoaded(this.userDetails);
@override
List<Object?> get props => [userDetails];
}
class UserDetailsError extends UserDetailsState {
final String message;
UserDetailsError(this.message);
@override
List<Object?> get props => [message];
}
my user details bloc
class UserDetailsBloc extends Bloc<UserDetailsEvent, UserDetailsState> {
UserDetailsBloc() : super(UserDetailsInitial());
Stream<UserDetailsState> mapEventtoState(UserDetailsEvent event) async* {
if (event is FetchUserDetails) {
yield UserDetailsLoading();
try {
final fetchedDetails = await fetchUserDetailsFromStorage(event);
print('user details fetched: $fetchedDetails');
yield UserDetailsLoaded(fetchedDetails.toJson());
} catch (e) {
yield UserDetailsError('Failed to fetch user details: $e');
}
}
}
Future<UserDetails> fetchUserDetailsFromStorage(
FetchUserDetails event) async {
// Implement the logic to fetch user details from secure storage or an API
// Return the fetched details
const storage = FlutterSecureStorage();
try {
String? storedUsername = await storage.read(key: 'userName');
String? storedPassword = await storage.read(key: 'pin');
if (storedUsername != null && storedPassword != null) {
// Create a UserDetails instance with the retrieved credentials
UserDetails userDetails = UserDetails(
username: storedUsername,
password: storedPassword,
deviceId: event.deviceId,
ipAddress: event.ipAddress,
appVersion: event.appVersion,
platform: event.platform,
);
// Fetch the user details from the repository
UserDetails fetchedUserDetails =
await UserDetailsRepository().getUserDetails(userDetails);
return fetchedUserDetails;
} else {
throw Exception('Stored credentials not found');
}
} catch (e) {
throw Exception('Failed to fetch user details: $e');
}
}
}