New post on /r/flutterdev subreddit:
<b>Flutter with 1 year - lesson learned and still struggling</b>
It's my first full-time programming with my colleague. Previously I just copy and paste javascript sometimes. At this work, the App has been 5k+ loc and 3k commit.Working with Flutter was mostly a fun time. As everyone will feel, the UI implementation was really natural.Bloc pattern (Library is not used) and Provider are used. When using Stream, especially if you don't care about dispose or cancel, it was difficult to fix this because the listener was alive with a memory leak.```dart class SomeBloc implements Disposable {final StreamController ctrl = StreamController();StreamSubscription subs;SomeBloc() { subs = repository.data.listen((x) { if (x > 3) ctrl.sink.add(x); }); }// 🚫⚠️🚫⚠️🚫⚠️ Alaways do cancel and close. dispose() async { await subs.cancel(); await ctrl.close(); } } ```In particular, after listening, I felt that the async operation is a very dangerous operation. I think a language that directly manages memory should care about resource release in like this.```dart // Stream with async operation is hard to debug 😭 var subs = someStream.listen.((x) { var url = await getFromFirestorePathToUrl(x); await player.load(url);// if you have to cancel an operation, it is tough to get correct. if (isPlayerPlaying) return; await
player.play(); }); ```RxDart was very helpful. In most cases, CombineLatest was used to receive multiple streams and deliver them to the view.```dart // ❤️ Rx combineLatest // ✌️ Miss js destructuring assignment // const [profile, content, isSelected] = snapshot.dataStreamBuilder<List>( stream: Rx.combineLatest3(profileBloc.profile, repository.content, bloc.isSheSelected, (a, b, c) => [a, b, c]), builder: (context, snapshot) { if (snapshot.hasData && snapshot.isNotEmpty) { Profile profile =
snapshot.data[0]; Content content =
snapshot.data[1]; bool isSelected =
snapshot.data[2]; return Post(profile, content, isSelected); } else { return CircularProgressIndicator(); } } ); ```I want to know it is passable to convert Stream<List<Stream<Object>>> to Stream<List<Object>>. If it can be, please let me know. Perhaps it is about traverse in functional programming.```dart // Is this passable? // traverse:: Stream<List<Stream<Object>>> -> Stream<List<Object>>Stream<List<MetaPost>> metaPosts(String id) => repository.metaPosts; Stream<Post> post(String id) =>
repository.post;Stream<List<Stream<Post>>> postFromMetaPosts(String id) { return metaPosts(id).map((metas) { return
metas.map((meta) { return post(
meta.id); }).toList(); }); }Stream<List<post>> postFromMetaPosts2(String id) { // ?? } ```Provider was mostly used for dependency injection, but it was inconvenient to distinguish between
context.read and
context.watch. I thought it would be nice to have a Provider that is only provided for DI use. Or maybe I should use another library or just use InheritedWidget.``dart // I always set providers ininitStatewithcontext.read` class _SomeStateWidgetState extends State<SomeStateWidget> { Repository _repo; AuthBloc _auth; SomeStateWidgetBloc _bloc;
@override void initState() { _repo =
context.read<Repository>(); _auth =
context.read<AuthBloc>(); _bloc =
context.read<SomeStateWidgetBloc>(); super.initState(); }
@override Widget build(BuildContext context) { return ListView( children: [ Post(_bloc.isSelected, _auth.currentUserId), // 👌safe _chip(), ] ); }Widget _chip() { return Chip( _auth.currentUserId, // 👌safe onTap: () async { bool isDeleted = await _repo.isDeleted(); // 👌safe print(isDeleted); }) } }// But still hard in stateless widget class SomeStatelessWidget extends StatelessWidget { const SomeStatelessWidget({Key key}) : super(key: key);
@override Widget build(BuildContext context) { var _auth =
context.watch<AuthBloc>(); // 👻watch var _bloc =
context.watch<SomeStatelessWidgetBloc>(); //…