How to change variable value in flutter with bloc?

1k Views Asked by At

Want to ask is How to change variable value with stream flutter?

You think my question is so fundamental and I can search in everywhere on internet. But in this scenario with stream, I can't change the variable value with method. How I need to do? please guide me. I will show with example.

Here, this is bloc class code with rxDart.

class ChangePinBloc {
  final ChangePinRepository _changePinRepository = ChangePinRepository();
 
 
  final _isValidateConfirmNewPinController = PublishSubject();
  
  String oldPin = '';
  

  Stream get isValidateConfirmNewPinStream =>
      _isValidateConfirmNewPinController.stream;


  void checkValidateConfirmNewPin(
      {required String newPinCode, required String oldPinCode}) {
    if (newPinCode == oldPinCode) {
      oldPin = oldPinCode;
      changePin(newCode: newPinCode);
      isValidateConfirmPin = true;
      
      _isValidateConfirmNewPinController.sink.add(isValidateConfirmPin);
    } else {
      isValidateConfirmPin = false;
      _isValidateConfirmNewPinController.sink.add(isValidateConfirmPin);
    }
  }

 

  void changePin({required String newCode}) async {
    changePinRequestBody['deviceId'] = oldPin;
   
  }

  dispose() {
   
  }
}

Above code, want to change the value of oldPin value by calling checkValidateConfirmNewPin method from UI. And want to use that oldPin value in changePin method. but oldPin value in changePin always get empty string.

This is the calling method checkValidateConfirmNewPin from UI for better understanding.

PinCodeField(
                            pinLength: 6,
                            onComplete: (value) {
                              pinCodeFieldValue = value;
                              changePinBloc.checkValidateConfirmNewPin(
                                  newPinCode: value,
                                  oldPinCode: widget.currentPinCodeFieldValue!);
                            },
                            onChange: () {},
                          ),

Why I always get empty String although assign a value to variable?

Lastly, this is complete code that calling state checkValidateConfirmNewPin from UI.

void main() {
  
  final changePinBloc = ChangePinBloc();

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: StreamBuilder(
          stream: changePinBloc.isValidateConfirmNewPinStream,
          builder: (context, AsyncSnapshot pinValidateSnapshot) {
            return Stack(
              children: [
                Positioned.fill(
                  child: Column(
                    children: [
                      const PinChangeSettingTitle(
                        title: CONFIRM_NEW_PIN_TITLE,
                        subTitle: CONFIRM_NEW_PIN_SUBTITLE,
                      ),
                      const SizedBox(
                        height: margin50,
                      ),
                      Padding(
                        padding: const EdgeInsets.only(
                            left: margin50, right: margin50),
                        child: PinCodeField(
                          pinLength: 6,
                          onComplete: (value) {
                            changePinBloc.checkValidateConfirmNewPin(
                              newPinCode: value,
                                oldPinCode: widget.newCodePinValue!,
                            );
                          },
                          onChange: () {},
                        ),
                      )
                    ],
                  ),
                ),
                pinValidateSnapshot.hasData
                    ? pinValidateDataState(pinValidateSnapshot, changePinBloc)
                    : const Positioned.fill(
                        child: SizedBox(),
                      ),
              ],
            );
          },
        ),
      ),
    );
  }
  
}

1

There are 1 best solutions below

1
Abdelaziz Rashed On

To update the variable you should emit a new state using emit() method.

Just make sure your bloc is correct as it should inherit from Bloc object. Read flutter_bloc documentation to know how to use it.

A simple example:

class ExampleBloc extends Bloc<ExampleEvent, ExampleState> {
   ExampleBloc() : super(ExampleInitial()) {
      on<ExampleEvent>((event, emit) {
         //Do some logic here
         emit(ExampleLoaded());
      });
   }
}