Inscription – sword tianya, start from your drip accumulation, and will keep improving.
Flutter is Google’s latest mobile development framework.
[X1] The daily reminder of wechat official account is accumulated at any time
【 X2 】 Various series of free open source video tutorials focus on you won’t get lost
【 X3 】 series article millions of Demo copy and paste use at any time
The schemes available for asynchronous communication in Flutter are as follows:
- Provider
- ValueNotifier Click to view details
- The use of the Stream: StreamController details | StreamBuilder components used in combination
- EventBus
- Bloc
Stream is an abstract class that represents a sequence of asynchronous data.
-
Single Subscription streams, which have a maximum of one listener
-
Broadcast streams, which can have multiple listeners
A Stream can accept any type of data. A Stream can be a synchronous Stream or an asynchronous Stream. The difference is that a synchronous Stream sends an event immediately after executing add, addError, or close, whereas an asynchronous Stream always sends an event after executing code in the event queue.
1 Multiple subscription streams
The first step is to create a stream object, which is operated on here by a StreamController, StreamController, which will be referred to as a stream in the following articles.
StreamController<String> streamController = StreamController.broadcast();
Copy the code
StreamSubscription = StreamSubscription = StreamSubscription = StreamSubscription = StreamSubscription = StreamSubscription = StreamSubscription = StreamSubscription = StreamSubscription The StreamSubscription object must be used to close the listener when the page is destroyed, otherwise the listener will not move and will leak memory. Listing 1-1 shows the core code:
/// Listing 1-1
class TestBPage extends StatefulWidget {
@override
State<StatefulWidget> createState(a) {
return_TestBPageState(); }}class _TestBPageState extends State {
/// Message order object
StreamSubscription _streamSubscription;
@override
void initState(a) {
super.initState();
/ / / listen 2
_streamSubscription=streamController.stream.listen((event) {
print("Page B receives data $event");
});
}
@override
void dispose(a) {
super.dispose();
/// cancel the listener_streamSubscription.cancel(); }... . }Copy the code
StreamSubscription is used to handle current listener events. If there are multiple listener events in a multi-subscription stream, there should be multiple StreamSubscription events. Each individual event listener needs to be handled using a StreamSubscription object, as described in table 3 of this article.
The third step is to send data, such as clicking a button to trigger or network request data results distributed to multiple pages, of course, this is based on the actual business.
streamController.add("Test data");
Copy the code
The fourth step is to close the stream after the entire operation is complete
@override
void dispose(a) {
super.dispose();
streamController.close();
}
Copy the code
2 Single subscription streams
A single subscription stream is only allowed to have one listener, which is created by default using the StreamController constructor as follows:
/// Create a single subscription flow controller
StreamController<String> streamSingController = StreamController();
Copy the code
Then set the listener to be consistent with the multi-subscription stream mode:
class _TestBPageState extends State {
/// Message order object
StreamSubscription _streamSubscription;
@override
void initState(a) {
super.initState();
/ / / listen 2
_streamSubscription=streamSingController.stream.listen((event) {
// Then update the data in this operation
print("Page B receives data $event");
});
}
@override
void dispose(a) {
super.dispose();
/// cancel the listener_streamSubscription.cancel(); }... . }Copy the code
When a listen listener is added to it, an exception is raised:
· · · · · · · · · · · · · · · · · · · · Builder: Bad state: Stream has already been listened to.Copy the code
3 StreamSubscription
StreamSubscription is used to operate on current listening events. It provides the following operations: Unsubscribe a message. This method is used during page destruction, after which the subscription listener is not reusable
@override
void dispose(a) {
super.dispose();
/// unsubscribe messages
_streamSubscription.cancel();
}
Copy the code
/// suspend message subscription
if(! _streamSubscription.isPaused){ _streamSubscription.pause(); }Copy the code
/// resume message subscription
if(_streamSubscription.isPaused){
_streamSubscription.resume();
}
Copy the code
The completion of
To xiaobian character, to achieve millions of Demo copy and paste at any time is certainly the need for source code
- Click for details
Of course, with the character of xiaobian, there must be a video recording, currently recording, you can pay attention to the watermelon video – the early young people will be uploaded later