simc / dartx

Superpowers for Dart. Collection of useful static extension methods.
https://pub.dev/packages/dartx
Apache License 2.0
1.09k stars 87 forks source link

Can I add ELEMENT between LIST of ELEMENTS? #64

Closed ianjaspersantos closed 4 years ago

ianjaspersantos commented 4 years ago
// List of Texts
final texts = <Widget>[
    Text('text1'),
    Text('text2'),
    Text('text3'),
];

// Extension Functionality
texts.insertDivider(Divider());

// Output
<Widget>[
    Text('text1'),
    Divider()
    Text('text2'),
    Divider()
    Text('text3'),
];

Is there any way I can achieve this using this packages easily? Thanks

xsahil03x commented 4 years ago

This package is specific to Dart extensions, flutter extensions should be added to "FlutterX".

The repository link is currently broken, @leisim will be able to guide you more on this.

passsy commented 4 years ago

I'm using this extension in my projects

import 'package:flutter/widgets.dart';
import 'package:dartx/dartx.dart';

extension WidgetListExt on Iterable<Widget> {
  /// adds widgets between the elements
  List<Widget> dividedWith(Widget divider) {
    final elements = flatMap((it) => [it, divider]).toList();
    // remove last divider
    return elements.dropLast(1);
  }
}
ianjaspersantos commented 4 years ago

Thanks for the answer. :) But somewhat somehow I think its relevant to dartx as it should be flexible as possible like the use case below:

final words = <String>[
    'Hello',
    'Flutter',
    'Community',
];

//Accepted value should be based on the generic class type passed
final newWords = words.dividedWith(' '); 

But ill the extension you provided. Thank you!

passsy commented 4 years ago

@ianjaspersantos You can use Iterable.joinToString()