Is there a naming convention for Dart packages? Is there maybe a document that describes patterns? I have trouble figuring out naming convention for package names that consist of multiple words. For example, should I use placeView
, PlaceView
, place_view
, or something else?
This is documented on the name section of Pubspec Format documentation.
It should be all lowercase, with underscores to separate words,
just_like_this
. Stick with basic Latin letters and Arabic digits:[a-z0-9_]
and ensure that it’s a valid Dart identifier (i.e. doesn’t start with digits and isn’t a reserved word).Try to pick a name that is clear, terse, and not already in use.
There doesn't seem to be a convention for it, but most of the time, I see lowercase_words_with_underscore
being used, both for libraries and packages.
For libraries inside packages, some people also use dots for grouping, for example my_package.lib_1
and my_package.lib_2
.
It's all personal preference, I guess.
You only can create flutter project by lowercase and no space between the project name and avoid to add special characters. Follow these steps you will not get any error like this.
"ProjectName" is not a valid Dart package name.
From the [Pubspec format description](https://www.dartlang.org/tools/pub/pubspec.html):
**DO** use `lowercase_with_underscores` for package names.
Package names should be all lowercase, with underscores to separate words,
`just_like_this`. Use only basic Latin letters and Arabic digits: [a-z0-9_].
Also, make sure the name is a valid Dart identifier -- that it doesn't start
with digits and isn't a reserved word.
-
) or some other non 0-9, a-z character, then rename the folder and try again –
Middleclass All the package conventions are documented on pub.dartlang.org. The package naming conventions in particular are documented on the pubspec format page.
I only found this https://code.google.com/p/dart/issues/detail?id=5094
- That the package name follows the naming conventions (valid Dart identifier, doesn't conflict with any reserved words, all lower case).
- Naming convention summary -
Dart uses 03 naming convention methods.
- UpperCamelCase
- lowerCamelCase
- lowercase_with_underscores
01. UpperCamelCase
Classes ( eg:- MyClass )
class MyClas { ... }
enum ( eg:- Status )
enum Status { none, running, stopped, paused }
typedefs ( eg:- Signature )
typedef Signature<T> = bool Function(T value);
type parameters ( eg:- T )
class Foo<T extends Object> { ... }
extension ( eg:- MyMethod )
extension MyMethod<T> on List<T> { ... }
02. lowercase_with_underscores
packages
eg:- hive_flutter, url_launcher, flutter_svg
directories (mostly lowercase only)
eg:- lib, bin, src, test, example
source files
eg:- main.dart, home.dart
import prefixes
eg:- import 'dart:math' as math;
eg:- import 'dart:math' as my_math;
03. lowerCamelCase
Class members (instance/static methods and variables)
eg:- void myFun() {}, int studentRank = 1;
top-level definitions
eg:- double topVariable;
variables
eg:- int myValue = 3;
parameters (both positional and named)
eg:- void fun({int myParam}){...} , void fun(int myParam) {...}
In your case (for packages), you have to use "lowercase_with_underscore".
I had the same issue.
If you are like me you will face a challenge if your app_name
begins with a digit.
My App name begins with a number so my solution was to change the number "1"
into the word "one"
Its some of these small differences you have to watch for.
© 2022 - 2024 — McMap. All rights reserved.