What is package naming convention used in Dart?
Asked Answered
F

7

43

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?

Formulism answered 28/1, 2014 at 9:11 Comment(0)
A
62

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.

Antemundane answered 28/1, 2014 at 9:26 Comment(2)
Actually this is documented. See the answer of @nex3Antemundane
Correct. Changing the directory to lower_case format helped.Corr
D
7

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.

Douai answered 28/1, 2014 at 9:29 Comment(2)
The style guide covers the names of libraries and source files but not the naming of packages. But I agree it's almost the same thing.Antemundane
@AlexandreArdhuin I totally got those two confused. But yeah, the lowercase_with_underscore holds true for both.Douai
L
5

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.
Loudspeaker answered 1/4, 2019 at 8:48 Comment(1)
flutter may use the folder name as the package name, so if your folder has a dash in it (-) or some other non 0-9, a-z character, then rename the folder and try againMiddleclass
M
4

All the package conventions are documented on pub.dartlang.org. The package naming conventions in particular are documented on the pubspec format page.

Mirthless answered 29/1, 2014 at 0:39 Comment(0)
B
1

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).
Brainstorm answered 28/1, 2014 at 9:18 Comment(0)
M
1

- Naming convention summary -

Dart uses 03 naming convention methods.

  1. UpperCamelCase
  2. lowerCamelCase
  3. 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".

Myrilla answered 4/3, 2022 at 6:15 Comment(0)
C
0

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.

Coulombe answered 5/2, 2022 at 9:15 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.