简体   繁体   中英

Dart formatting issue [VSCODE]

Could anyone explain to me why dart formats the code in a way it does it? I have the dart plugin installed and have used the 'Dart: Use recommended settings'.

Future<void> addProduct(Product product) {
    return http
        .post(
      '$url.json',
      body: json.encode(
        {
          'title': product.title,
          'description': product.description,
          'imageUrl': product.imageUrl,
          'price': product.price,
          'isFavorite': product.isFavorite,
        },
      ),
    )
        .then((response) {
      var id = json.decode(response.body)['name'];
      var newProduct = Product(
        description: product.description,
        title: product.title,
        price: product.price,
        imageUrl: product.imageUrl,
        id: id,
      );
      _items.add(newProduct);
      notifyListeners();
    }).catchError((err) {});
  }

As mentioned in the comments, the Dart extension just delegates formatting to dartfmt so the formatting is based on its conventions, including 2-spaces for indenting and 4-space indenting for line continuations.

dartfmt is an opinionated formatter so there's not much that can be controlled, though trailing commas will affect how it formats some code:

https://flutter.dev/docs/development/tools/formatting

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM