Dart-code: Widget instantiation indents first named argument by 4 spaces in stead of two

Created on 2 Sep 2019  路  3Comments  路  Source: Dart-Code/Dart-Code

Hi there, I did see the issue where this cannot work with tabs but I am using 2 spaces, so I think I should be fine. https://github.com/Dart-Code/Dart-Code/issues/1015

Anyways, the issue im seeing is that when you're initializing a widget, the first argument indents by 4 spaces, and makes it look a little off. Here's a single file I've been working on that shows it:

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    // TODO: implement createState
    return _MyAppState();
  }
}

class _MyAppState extends State<MyApp> {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        home: Scaffold(
      appBar: AppBar(title: Text('EasyList')),
      body: Column(children: <Widget>[
        Container(
          margin: EdgeInsets.all(10.0),
          child: RaisedButton(
            onPressed: () {},
            child: Text('Add Product'),
          ),
        ),
        Card(
            child: Column(
          children: <Widget>[
            Image.asset('assets/food.jpg'),
            Text('Food Paradise'),
          ],
        )),
      ]),
    ));
  }
}

Can see that home: Scaffold( is indented one too far, and that kind makes the nested arguments below it look weird too.

I've also captured the logs here in case it's helpful.

Dart-Code-Log-2019-08-01 14-49-12.txt

working as intended

Most helpful comment

I believe this is caused by a missing trailing comma near the end of your code:

    ));
  }
}

Without trailing commas dartfmt indents wrapped lines using 4 spaces. Here your code has a mix, so you end up with some lines formatted one way and some the other.

Let me know if this doesn't help!

All 3 comments

I believe this is caused by a missing trailing comma near the end of your code:

    ));
  }
}

Without trailing commas dartfmt indents wrapped lines using 4 spaces. Here your code has a mix, so you end up with some lines formatted one way and some the other.

Let me know if this doesn't help!

It does thank you! Is there a way to have it auto add trailing commas?

Great! :-)

I don't know of one - there's some discussion over at https://github.com/dart-lang/sdk/issues/27084 about this, but I think it's hard to know when commas should/shouldn't be added. I'd recommend subscribing to and/or commenting on that issue, since any fix for this would need to come from the SDK or dartfmt.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

FeimiSzy picture FeimiSzy  路  4Comments

lukepighetti picture lukepighetti  路  4Comments

rajeshjeshar picture rajeshjeshar  路  4Comments

jascodes picture jascodes  路  4Comments

shamrin picture shamrin  路  5Comments