Application/medcify/lib/pages/main/qrcode/qr_code_bloc.dart

49 lines
1.3 KiB
Dart
Raw Normal View History

2022-09-26 06:33:52 +00:00
import 'dart:convert';
import 'dart:typed_data';
import 'package:flutter_bloc/flutter_bloc.dart';
import 'package:medcify/models/order_item.dart';
import 'package:medcify/network/api_provider.dart';
import '../../../components/alert.dart';
import '../../../constants.dart';
import '../../../helpers.dart';
import '../../../models/banner_item.dart';
import '../../../models/medicine_item.dart';
import '../../../navigation/navigation.dart';
abstract class QrCodeState{}
class QrCodeInitial extends QrCodeState{}
class QrCodeLoading extends QrCodeState{}
class QrCodeSuccess extends QrCodeState{}
class QrCodeFailure extends QrCodeState{
String error;
QrCodeFailure(this.error);
}
class QrCodeCubit extends Cubit<QrCodeState> {
QrCodeCubit() : super(QrCodeInitial());
Uint8List? image;
String name = "";
String url = "";
Future<void> fetchQrCode() async{
emit(QrCodeLoading());
final response = await ApiProvider.instance.fetchQrCode();
if(response.status ?? false){
name = response.name ?? "";
url = response.url ?? "";
String data = response.image ?? "";
if(data.isNotEmpty){
image = const Base64Decoder().convert(data.split(',').last);
}
emit(QrCodeSuccess());
}else{
emit(QrCodeFailure(response.message ?? "Something went wrong"));
}
}
}