40

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?

nbro
  • 15,395
  • 32
  • 113
  • 196
markovuksanovic
  • 15,676
  • 13
  • 46
  • 57

7 Answers7

60

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.

Community
  • 1
  • 1
Alexandre Ardhuin
  • 71,959
  • 15
  • 151
  • 132
6

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.

MarioP
  • 3,752
  • 1
  • 23
  • 32
  • 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. – Alexandre Ardhuin Jan 28 '14 at 09:44
  • @AlexandreArdhuin I totally got those two confused. But yeah, the lowercase_with_underscore holds true for both. – MarioP Jan 28 '14 at 10:26
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.
Hemant Sharma
  • 259
  • 5
  • 14
  • 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 again – Brad Parks Nov 15 '20 at 01:43
4

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

Natalie Weizenbaum
  • 5,884
  • 28
  • 22
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).
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
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".

dilshan
  • 2,045
  • 20
  • 18
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.

Ace
  • 390
  • 1
  • 4
  • 11