flutter 横向扩展_Flutter有用的扩展

flutter 横向扩展

I have an Android 🤖 dev background. So when I started to write in Flutter there were, of course, things that I could do in Kotlin but there were not possible in Dart. I’ve started to write my own extension that could bring a little bit of that back, but also others that help me in day to day coding.

我有Android🤖开发背景。 因此,当我开始用Flutter进行写作时,当然可以在Kotlin中做一些事情,但是在Dart中是不可能的。 我已经开始编写自己的扩展程序,可以带回一点点,还有其他可以帮助我进行日常编码的扩展。

This will be a couple of them. Let me know what do you think and maybe if you have your own and would like to share them in comments that would be great 😃

这将是其中的几个。 让我知道您的想法,也许如果您有自己的想法,并希望在评论中分享,那就太好了😃

Kotlin (Kotlin)

So in Kotlin you write let/apply/also/run etc. almost all the time. So what I came up with are implementations of let and also.

因此,在Kotlin中,几乎所有时间都编写let / apply / also / run等。 因此,我想到的是let和also的实现。

extension KotlinExt<T> on T {
 A let<A>(A Function(T) f) => f(this);
 T also(void Function(T) f) {
    f(this);
    return this;
  }
}

用法示例: (Example of usage:)

In BlocListener you could do:

在BlocListener中,您可以执行以下操作:

listener: (context, state) {
  state.dialogError?.let((it) => showErrorDialog(it, context));
},

You would like to return filtered list but also assign it to the field:

您想返回过滤列表,但还要将其分配给该字段:

List<> filter(...) => list.where(...)
  .also((it) {
    _currentList = it;
  });

Of course, “it” could be something more meaningful but for the sake of example, it will do.

当然,“ 它”可能更有意义,但是为了举例说明,它会起作用。

清单🗄 (List 🗄)

In Flutter we use the lists on a daily basis, in widgets tree via children param or just a list of data.

在Flutter中,我们每天通过参数或仅在数据列表中使用小部件树中的列表。

extension ListExt<T> on List<T> {
  List<T> copy() => List.from(this, growable: true);


  List<Y> mapIndexed<Y>(Y Function(T, int) onMap) =>
      asMap().entries.map((entry) =>
          onMap(entry.value, entry.key)).toList();
}

用法示例: (Example of usage:)

Ok, so the state (eg. in Bloc) should be immutable, why? Oh boy, that needs a separate explanation for example from here. But let go back on tracks, if the state is immutable then lists in it are too. So when we want to change them we need to create a copy of them and not just add/remove on them.

好的,所以状态(例如在Bloc中)应该是不变的,为什么? 哦,男孩, 这里需要例如单独的说明。 但是,让我们回到正轨,如果状态是不可变的,那么其中的列表也是如此。 因此,当我们要更改它们时,我们需要创建它们的副本,而不仅仅是在它们上添加/删除。

yield state.copyWith(
        favorites: state.favorites.copy()..add(workoutId));

If we would like to do some custom avatar list like:

如果我们想做一些自定义头像列表,例如:

Image for post

Then “mapIndexed” (which is also in Kotlin) is perfect:

那么“ mapIndexed ”(也在Kotlin中)是完美的:

List<Widget> _avatars() => urls.mapIndexed((url, index) {
        final padding = _spaceBetween * index;
        return Positioned(
          top: 0.0,
          left: 0.0 + padding,
          child: Avatar(
            size: size,
            imageUrl: url,
          ),
        );
      });

可迭代的 (Iterable)

So to be more flexible I add also extensions for Iterable.

为了更加灵活,我还添加了Iterable的扩展。

extension IterableExt<T> on Iterable<T> {
  bool containsAll(Iterable<T> list) {
    for (final item in list) {
      if (!contains(item)) return false;
    }
    return true;
  }


  T findOrNull(bool Function(T) where) => firstWhere(where, orElse: () => null);
}

用法示例: (Example of usage:)

ContainsAll is great when you want to for example implement filtering by many categories.

当您想要例如按许多类别实施过滤时, ContainsAll非常有用。

final filtered = _currentChallenges
        .where((it) => it.categories.containsAll(filters));

FindOrNull is just a shorter notation then using orElse with lambda.

FindOrNull只是将orElse与lambda结合使用时的缩写

final workout = _currentWorkouts.findOrNull((it) => it.id == workoutId);

那是所有人😃 (That's all folks 😃)

Those are just some of my extensions I’m using on a daily basis. If you liked it please leave 👏.If you have your set that you can share please do in the comments.Thank you for reading 👋.

这些只是我每天使用的一些扩展程序。 如果您喜欢它,请离开👏。如果您有可以共享的布景,请在评论中进行。感谢您阅读👋。

翻译自: https://medium.com/swlh/flutter-useful-extensions-a2796a42bde1

flutter 横向扩展

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值