CallDetectorMixin mixin
CallDetectorMixin provides a convenient way to enable call detection functionality in any class without needing to directly instantiate or manage a CallDetector instance.
This mixin gives access to the singleton instance of CallDetector and exposes all of its public methods and getters. Classes that mix this in can easily check call status, subscribe to call events, and manage the detector’s lifecycle without knowing the singleton’s internal details.
Ideal for use in layers such as Provider, Bloc, Cubit, StatefulWidget,
StatelessWidget, or any service class that needs call detection functionality.
Example usage:
class MyBloc extends Bloc<MyEvent, MyState> with CallDetectorMixin {
MyBloc() : super(MyState()) {
on<_WatchDetector>(_watchDetector);
on<_GetCurrentCallStatus>(_getCurrentCallStatus);
add(_WatchDetector());
}
Future<void> _watchDetector(_WatchDetector event, Emitter<MyState> emit) {
return emit.forEach(
callDetectStream,
onData: (callStatus) {
print('Current call status: $callStatus');
return state.copyWith(isInCall: callStatus);
},
);
}
Future<void> _getCurrentCallStatus(_GetCurrentCallStatus event, Emitter<MyState> emit) async {
// ...
final currentCallValue = await getCurrentCallStatus();
// ...
}
}
class MyCubit extends Cubit<MyState> with CallDetectorMixin {
StreamSubscription<bool>? subscription;
MyCubit() : super(const MyState());
void init() {
subscription ??= callDetectStream.listen((isCalling) {
if (isCalling) {
print('Call is active in MyCubit!');
}
});
}
Future<void> checkStatus() async {
bool status = await getCurrentCallStatus();
print('Current call status: $status');
}
@override
Future<void> close() async {
await subscription?.cancel();
return super.close();
}
}
class MyWidget extends StatefulWidget {
const MyWidget({super.key});
@override
State<MyWidget> createState() => _MyWidgetState();
}
class _MyWidgetState extends State<MyWidget> with CallDetectorMixin {
@override
Widget build(BuildContext context) {
return StreamBuilder<bool>(
stream: singletonCallDetectStream,
builder: (context, snapshot) {
return Text('Current call status: ${snapshot.data}');
}
);
}
}
class MyWidget extends StatelessWidget with CallDetectorMixin {
const MyWidget({super.key});
@override
Widget build(BuildContext context) {
return StreamBuilder<bool>(
stream: singletonCallDetectStream,
builder: (context, snapshot) {
return Text('Current call status: ${snapshot.data}');
}
);
}
}
Properties
-
callDetectStream
→ Stream<
bool> -
Returns a broadcast stream of events that signal changes
in call status.
no setter
- hashCode → int
-
The hash code for this object.
no setterinherited
- hasListeners → bool
-
Indicates whether there are active listeners on the call detection stream.
no setter
- isStreamActive → bool
-
Indicates whether the call detection stream is currently active.
no setter
- lastCallStatus → bool
-
Returns the most recent known call status.
no setter
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
-
singletonCallDetectStream
→ Stream<
bool> -
Returns a broadcast stream of events that signal changes
in call status.
no setter
Methods
-
getCurrentCallStatus(
) → Future< bool> - Asynchronously fetches the current call status from the platform.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited