I am starting with Flutter code and I understand that a good practice is to add a comma at the end of the code, this in order to have a good code format, but when I add a comma and make a line break, the code formatting removes the line break and the comma, how can I correct this behavior?:
import 'package:flutter/material.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
void main() {
runApp(ProviderScope(child: App()));
}
class App extends HookConsumerWidget {
const App({super.key});
@override
Widget build(BuildContext context, WidgetRef ref) {
return MaterialApp(
debugShowCheckedModeBanner: false,
title: 'Daha',
home: Scaffold(body: Center(child: Text('Hello world'))),
);
}
}
2
Answers
You can change or disable formatting options from the settings.json config file in .vscode. See for example, and this one.
The suggestion is partially correct but overuses commas. Adding a comma after every closing bracket is unnecessary. Trailing commas should only be added where they help the Dart formatter preserve multi-line formatting.
Add a trailing comma only after the outermost closing brackets of widgets that should be formatted across multiple lines.
Avoid adding commas after inner brackets unless required for lists or complex widget structures.
something like this:
home: Scaffold(
body: Center(
child: Text(‘Hello world’),
),
),
Unnecessary Commas: Adding commas after every bracket is excessive and not needed unless dealing with lists or maintaining complex layouts.