I was watching a tutorial about bloc and stream subscription and I noticed this piece of code. Right after the super constructor, he called a function.
class InternetCubit extends Cubit<InternetState> {
final Connectivity connectivity;
late StreamSubscription connectivityStreamSubscription;
InternetCubit({required this.connectivity}) : super(InternetLoading()) {
connectivityStreamSubscription = connectivity.onConnectivityChanged.listen((ConnectivityResult connectivityResult) {
if(connectivityResult == ConnectivityResult.wifi){
emitInternetConnected(ConnectionType.Wifi);
}
else if(connectivityResult == ConnectivityResult.mobile){
emitInternetConnected(ConnectionType.Mobile);
}else if(connectivityResult == ConnectivityResult.none){
emitDisconnected();
}
});
}
}
Can anyone explains to me what this function after the super constructor is called? And what does it do?
Can anyone explains to me what this function after the super constructor is called?
This is not a function, it's just a constructor's body: https://dart.dev/guides/language/language-tour#constructors
And what does it do?
In this case, during the InternetCubit
initialization, cubit subscribes to
connectivity.onConnectivityChanged
stream events. Then, on each connectivityResult
event, the corresponding cubit state change is triggered. This is useful when your cubit state should change on custom cubit events (e.g. when you call a cubit method from UI) as well as other event sources e.g. internet connectivity status provided by the connectivity
package.