Flutter withdraw money from a budget Object using bloc

Viewed 32

I have a app that is for budgeting. I'm trying to create a Bloc that can withdraw money from a Budget Object. However I'm running into issues trying to figure out how to define the bloc in a way that it can withdraw the money and pass an update budget object back.

BudgetBloc

import 'package:bloc/bloc.dart';
import 'package:budget_app/model/budget.dart';
import 'package:budget_app/model/budget_repository.dart';
import 'package:equatable/equatable.dart';

part 'budget_event.dart';
part 'budget_state.dart';

class BudgetBloc extends Bloc<BudgetEvent, BudgetState> {
  final BudgetRepository budgetRepository;

  BudgetBloc({required this.budgetRepository}) : super(BudgetInitial()) {
    on<AppStarted>((event, emit) async {
      emit(BudgetLoading());
      if (await budgetRepository.hasBudget()) {
        var budget = await budgetRepository.getBudget();
        emit(BudgetLoaded(budget: budget));
      } else {
        emit(BudgetNew());
      }
    });
  }
}

BudgetEvent

part of 'budget_bloc.dart';

abstract class BudgetEvent extends Equatable {
  const BudgetEvent();

  @override
  List<Object> get props => [];
}

class AppStarted extends BudgetEvent {}

class Withdraw extends BudgetEvent {}

BudgetState

part of 'budget_bloc.dart';

abstract class BudgetState extends Equatable {
  const BudgetState();

  @override
  List<Object> get props => [];
}

class BudgetInitial extends BudgetState {}

class BudgetLoading extends BudgetState {}

class BudgetNew extends BudgetState {}

class BudgetLoaded extends BudgetState {
  final Budget budget;

  BudgetLoaded({required this.budget});
}

Budget is a simple class essentially just has a value and a name. It has functions for withdrawing and depositing money.

I thought about passing the budget back into the Withdraw method like such

Withdraw(Double amount, Budget budget) 

then maybe Withdraw just emits a BudgetLoaded Object with the updated budget?

0 Answers
Related