简体   繁体   中英

Access StreamController Stream from multiple widgets

Hi I am new to flutter and Streams, BLoC, and providers.

I am trying to access a stream from two different widgets. While it works in one widget it does not in the other. I have separate stream builders in both widgets which I think could be the problem. Why is it not working? What am I doing wrong and not understanding?

BloC with stream controllers and streams:

String dialCode = "";

class LogInController {
  final dialCodeStreamController = StreamController<String>();
  Stream<String> get dialCodeStream => dialCodeStreamController.stream;

  final dialCodeUpdateStreamController = StreamController<String>();
  Sink<String> get dialCodeSink => dialCodeUpdateStreamController.sink;
  Stream<String> get dialCodeUpdateStream =>
      dialCodeUpdateStreamController.stream;

  LogInController() {
    dialCodeUpdateStream.listen((updatedDialCode) {
      dialCode = updatedDialCode;
      dialCodeStreamController.add(dialCode);
    });
  }

  void dispose() {
    dialCodeStreamController.close();
    dialCodeUpdateStreamController.close();
  }
}

Widget where stream is working:

class CountryCodeWidget extends StatelessWidget {


final LogInController controller = LogInController();
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(12, 12, 12, 8),
      child: Card(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Text(
                selectCountryCodeString,
                style: hintTextStyle,
              ),
              StreamBuilder<String>(
                  stream: controller.dialCodeStream,
                  initialData: "",
                  builder: (context, snapshot) {
                    return Container(
                      decoration: BoxDecoration(
                        border: Border(
                          bottom: BorderSide(
                            color: snapshot.data == ""
                                ? whiteColor
                                : secondaryColor,
                            width: 4,
                          ),
                        ),
                      ),
                      child: Padding(
                        padding: const EdgeInsets.all(4.0),
                        child: Text(
                          '${snapshot.data}',
                          style: textStyle,
                        ),
                      ),
                    );
                  }),
              FlatButton(
                color: secondaryColor,
                child: Icon(
                  Icons.keyboard_arrow_down,
                  color: textColor,
                ),
                onPressed: () => countryCodeDialog(context),
              ),
            ],
          ),
        ),
      ),
    );
  }

Widget where stream is not working:

class MobileNumberWidget extends StatelessWidget {
  final LogInController controller = LogInController();
  final mobileNumberController = TextEditingController();
  @override
  Widget build(BuildContext context) {
    return Container(
      margin: const EdgeInsets.fromLTRB(12, 0, 12, 8),
      child: Card(
        child: Padding(
          padding: const EdgeInsets.all(8.0),
          child: Row(
            mainAxisSize: MainAxisSize.max,
            mainAxisAlignment: MainAxisAlignment.spaceEvenly,
            children: <Widget>[
              Expanded(
                child: Padding(
                  padding: const EdgeInsets.all(4.0),
                  child: TextField(
                    controller: mobileNumberController,
                    keyboardType: TextInputType.phone,
                    style: textStyle,
                    decoration: InputDecoration(
                      hintText: mobileNumberHintString,
                      hintStyle: hintTextStyle,
                      enabledBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                          color: hintTextColor,
                          width: 1,
                        ),
                      ),
                      focusedBorder: UnderlineInputBorder(
                        borderSide: BorderSide(
                          color: textColor,
                          width: 2,
                        ),
                      ),
                    ),
                  ),
                ),
              ),
              StreamBuilder<String>(
                  stream: controller.dialCodeStream,
                  initialData: "",
                  builder: (context, snapshot) {
                    return FlatButton(
                      child: Icon(
                        Icons.done,
                        color: textColor,
                      ),
                      onPressed: () => mobileNumberController.text.length == 10
                          ? snapshot.data == ""
                              ? showSnackBar(
                                  context, selectCountryCodeSnackBarString)
                              : //sendOTP(mobileNumberController.text)
                              showSnackBar(context, "YEs")
                          : showSnackBar(context, mobileNumberLengthString),
                    );
                  }),
            ],
          ),
        ),
      ),
    );
  }
}

Let me know if I can explain my question better or if I am not making sense. Thank you.

Found the problem and solution. Initialising stream controller twice was the problem. I initialised in the parent widget and passed it down to the child widgets.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM