Dedent - Remove any common leading whitespace from every line in text. Ported from Python.



  dedent: any

to your pubspec.yaml then run the usual flutter pub get or just pub get for a pure dart project.


import 'package:dedent/dedent.dart';

It's quite handy to strip leading spaces from multi-line text. Flutter code often involves deeply nested indented code, so any multiline text will end up containing leading spaces or tabs - which may affect your layout!

  child: Text(dedent('''
    Not really possible to access another widget, or rather, another
    widget's state in order to change it. You really need to create
    a model and have the model change cause the change.

dedent() removes any common leading whitespace from every line in text. This can be used to make triple-quoted strings line up with the left edge of the display, while still presenting them in the source code in indented form.

more specifically

    var text = '''
        def foo():
            while 1:
                return foo

    var _expect = '''
def foo():
    while 1:
        return foo

    expect(_expect, equals(dedent(text)));

Note that dedent() is ported from the long standing and reliable Python textwrap.dedent algorithm, complete with the original unit tests, so you know its good. πŸ‘


This project is a direct translation of the Python textwrap.dedent algorithm.

Tests are also ported from Python.

flutter test


This section is only applicable if you want to fiddle with the dedent source code

When checking out from ensure you specify dedent as a target so that the package directory is named dedent not flutter-dedent. Viz.

git clone dedent

If you have checked out the dedent project locally and prefer your other Dart/Flutter projects to refer to this local copy instead of to the official version of dedent on, simply adjust your pubspec.yaml:

    path: ../dedent

which assumes a directory structure where you are referring to the local dedent project from a sibling project e.g. called flutter_proj1 like this:

β”‚Development Directoryβ”‚
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
β”œβ”€β–Άβ”‚  flutter_proj1   β”‚
β”‚  β””β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”˜
β”‚  β”Œβ”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”€β”
└─▢│      dedent      β”‚

Thanks to the Python community for this algorithm.