did_change_dependencies
return a stream that emits null and done event when didchangedependencies is called for the first time.
author: petrus nguyễn thái học
import 'dart:async';
import 'package:did_change_dependencies/did_change_dependencies.dart';
import 'package:flutter/material.dart';
import 'package:rxdart_ext/rxdart_ext.dart';
class bloc {
stream<string> message$ = stream.empty(); // just demo
}
class mypage extends statefulwidget {
const mypage({key? key}) : super(key: key);
@override
_mypagestate createstate() => _mypagestate();
}
class _mypagestate extends state<mypage> with didchangedependenciesstream {
final bloc = bloc();
final subscriptions = <streamsubscription<void>>[];
@override
void initstate() {
super.initstate();
final handle = (string msg) {
// safe to access scaffoldmessengerstate.
scaffoldmessenger.of(context).showsnackbar(
snackbar(content: text(msg)),
);
};
subscriptions <<
didchangedependencies$
.exhaustmap((value) => bloc.message$)
.listen(handle);
subscriptions <<
didchangedependencies$.listen((event) {
// do something when `didchangedependencies` is called for the first time.
});
}
@override
void dispose() {
super.dispose();
subscriptions.foreach((s) => s.cancel());
}
@override
widget build(buildcontext context) {
return container();
}
}
extension _listext<t> on list<t> {
void operator <<(t t) => add(t);
}
Comments are closed.