mirror of
https://gitlab.com/comunic/comunicmobile
synced 2024-11-22 21:09:21 +00:00
46 lines
989 B
Dart
46 lines
989 B
Dart
import 'dart:async';
|
|
|
|
import 'package:comunic/helpers/events_helper.dart';
|
|
import 'package:flutter/material.dart';
|
|
|
|
/// Little State hack to avoid issues
|
|
///
|
|
/// @author Pierre HUBERT
|
|
|
|
abstract class SafeState<T extends StatefulWidget> extends State<T> {
|
|
final _subscriptions = List<StreamSubscription>();
|
|
|
|
bool _unmounted = false;
|
|
|
|
@override
|
|
void dispose() {
|
|
_unmounted = true;
|
|
|
|
// Close subscriptions
|
|
_subscriptions.forEach((f) => f.cancel());
|
|
|
|
super.dispose();
|
|
}
|
|
|
|
@override
|
|
void setState(fn) {
|
|
if (mounted && !_unmounted) super.setState(fn);
|
|
}
|
|
|
|
/// Register to a new subscription
|
|
@protected
|
|
void listen<T>(void onEvent(T event)) {
|
|
_subscriptions.add(EventsHelper.on<T>(onEvent));
|
|
}
|
|
|
|
/// Register to a new subscription
|
|
///
|
|
/// Callback will we called inside of setState
|
|
@protected
|
|
void listenChangeState<T>(void onEvent(T event)) {
|
|
_subscriptions.add(EventsHelper.on<T>((d) {
|
|
setState(() => onEvent(d));
|
|
}));
|
|
}
|
|
}
|