[英]Access StreamController Stream from multiple widgets
Hi I am new to flutter and Streams, BLoC, and providers.嗨,我是 flutter 和 Streams、BLoC 和提供商的新手。
I am trying to access a stream from two different widgets.我正在尝试从两个不同的小部件访问 stream。 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.
我在两个小部件中都有单独的 stream 构建器,我认为这可能是问题所在。 Why is it not working?
为什么它不起作用? What am I doing wrong and not understanding?
我做错了什么而不理解?
BloC with stream controllers and streams:带有 stream 控制器和流的 BloC:
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: stream 工作的小部件:
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: stream 不工作的小部件:
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.
初始化 stream controller 两次是问题所在。 I initialised in the parent widget and passed it down to the child widgets.
我在父小部件中初始化并将其传递给子小部件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.