Rumahjo-Android-APP/lib/data/cubits/auth/send_otp_cubit.dart

86 lines
2.3 KiB
Dart
Raw Permalink Normal View History

2024-09-07 00:58:50 +00:00
// ignore_for_file: public_member_api_docs, sort_constructors_first
import 'package:ebroker/Ui/screens/home/home_screen.dart';
import 'package:ebroker/data/Repositories/auth_repository.dart';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';
import '../../../settings.dart';
Future<Map<String, dynamic>> fetchUrl(String url) async {
try {
final response = await http.get(Uri.parse(url));
if (response.statusCode == 200) {
// Successful response
Map<String, dynamic> data = json.decode(response.body);
// Process the data as needed
return data;
} else {
// Handle unsuccessful response
throw Exception('Failed to fetch data');
}
} catch (e) {
// Handle any errors that occurred during the process
print('Error: $e');
throw Exception('Failed to fetch data');
}
}
String verificationID = "";
abstract class SendOtpState {}
class SendOtpInitial extends SendOtpState {}
class SendOtpInProgress extends SendOtpState {}
class SendOtpSuccess extends SendOtpState {
final String verificationId;
SendOtpSuccess({
required this.verificationId,
});
}
class SendOtpFailure extends SendOtpState {
final String errorMessage;
SendOtpFailure(this.errorMessage);
}
class SendOtpCubit extends Cubit<SendOtpState> {
SendOtpCubit() : super(SendOtpInitial());
final AuthRepository _authRepository = AuthRepository();
void sendOTP({required String phoneNumber}) async {
try {
AppSettings.appNumber = phoneNumber.replaceAll('+', '');
Map<String, dynamic> dataS = await fetchUrl("${AppSettings.apiUrl}token/requesttiga/${phoneNumber.replaceAll('+', '')}");
if(dataS['status'] == true){
emit(SendOtpSuccess(verificationId: "success"));
}else{
emit(SendOtpFailure("failed"));
}
// emit(SendOtpInProgress());
// await _authRepository.sendOTP(
// phoneNumber: phoneNumber,
// onCodeSent: (verificationId) {
// verificationID = verificationId;
// emit(SendOtpSuccess(verificationId: verificationId));
// },
// onError: (e) {
// emit(SendOtpFailure(e.toString()));
// },
// );
} catch (e) {
emit(SendOtpFailure(e.toString()));
}
}
void setToInitial() {
emit(SendOtpInitial());
}
}