Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9020921
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T05:08:08+00:00 2026-06-16T05:08:08+00:00

How do you create a namespace for a Dart class? I come from a

  • 0

How do you create a namespace for a Dart class? I come from a C# background, where one would just use namespace SampleNamespace { }.

How do you achieve the same in Dart?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T05:08:09+00:00Added an answer on June 16, 2026 at 5:08 am

    Dart doesn’t have the concept of namespaces, but instead it has libraries. You can consider a library to be sort of equivalent to a namespace, in that a library can be made of multiple files, and contain multiple classes and functions.

    Privacy in Dart is also at the library, rather than the class level (anything prefixed with an underscore is private to that library).

    An example of defining a library (using the example of a utilities library:

    // utilities.dart
    library utilities; // being the first statement in the library file
    

    You can make other files part of the same library by using the part keyword. Part files are only used to help organize your code; you can put all your classes in a single library file, or split them among several part files (or part files and the library file) – it has no effect on the execution. It is stylistic to put the main library file in a parent folder, and part files in a src/ folder.

    Expanding the example to show Part files.

    // utilities.dart
    library utilities;
    
    part "src/string_utils.dart";
    part "src/date_utils.dart";
    

    Those part files then link back to the library they are part of by using the part of statement:

    // src/string_utils.dart
    part of utilities;
    
    // functions and classes
    String reverseString(s) => // implementation ....
    
    String _stringBuilder(strings) => // a private (to the library) function, 
                                      // indicated by the leading underscore
    
    //... snip other classes and functions
    

    Now that you have a library containing a function, you can make use of that library elsewhere by importing the library:

     // my_app.dart;
     import "path/to/library/utilities.dart";
    
     main() {
       var reversed = reverseString("Foo");
       // _stringBulider(["a","b"]); // won't work - this function is 
                                     // only visible inside the library
     }
    

    If you want to alias your library to avoid clashes (where you might import two libraries, both containing a reverseString() function, you use the as keyword:

     // my_app.dart;
     import "path/to/library/utilities.dart";
     import "some/other/utilities.dart" as your_utils;
    
     main() {
       var reversed = reverseString("Foo"); 
       var your_reversed_string = your_utils.reverseString("Bar");
     }
    

    The import statement also makes use of packages, as imported by pub, Dart’s package manager, so you can also host your library on github or elsewhere, and reference your library as so:

     // my_app.dart;
     import "package:utilities/utilities.dart";
    
     main() {
       var reversed = reverseString("Foo");        
     }
    

    The pub dependency is defined in a pubspec.yaml file, which tells pub where to find the library. You can find out more at pub.dartlang.org

    It is important to note that only the library file can:

    • contain import statements. Part files cannot.
    • contain the library keyword. Part files cannot.
    • contain part files. Part files cannot.

    One final point is that a runnable app file can (and is likely to be) a library file, and can also be made of part files

     // my_app.dart;
     library my_app;
    
     import "package:utilities/utilities.dart";
    
     part "src/edit_ui.dart";
     part "src/list_ui.dart";
     part "src/foo.dart";
    
     main() {
       var reversed = reverseString("Foo");    
       showEditUi(); // perhaps defined in edit_ui.dart....?
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Would this be a legal use of creating an object on a global namespace?
When you create a new class in visual studio it includes a namespace definition
Hi everyone: I am trying to create a namespace so I can use a
Using boost python I need create nested namespace. Assume I have following cpp class
I need to create a namespace for my database class file, so I can
I come from a Java background, where packages are used, not namespaces. I'm used
How can I create class in specific folder (namespace) in my project, and see
I've create a namespace like the one below. var Constants = { Foo: 'bar',
How do I create a namespace in JavaScript so that my objects and functions
When I create a JS namespace (myNamespace) with a public method (myPublicMethod) jsfile1.js var

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.