Dart class dot notation like JS objects

Viewed 374

We switched to Flutter due to the instability of React Native. I cannot describe the happiness of working with a stable resource.

I created a structure for myself in React Native. I was able to access the pages with the "dot" notation both in JSX and in functions by creating static under a main class of components. How healthy the suspect is. Like this: <Component.Form.Input />

Since darts is purely an object language, I could not find an example of this type of structure. Even if I connect the static design of my root class element with the "Singleton" design pattern to the instance, I cannot access the methods below the linked class.

Helpers.Responsive.calcSome(params..)

In the example above, Helper is a class, Responsive is a separate class, and Helper is imported and assigned as a static variable. The calcSome function is a method of the Responsive class. How can I do this with Dart?

This is exactly what I want to do. You can think of each other as a chained class.

Of course, I do not know how healthy this approach will be for a clean code.

If you have different approaches that can work this way, please share as an alternative. I have an idea of how I should proceed.

Thank you for interest.

1 Answers

TL;DR: Not possible or not useful in your case. Namespaces don't exist per se in Dart

Long version

  • Only variables or methods can be dot accessed. See documentation for Classes and Operators.
  • The only approach I can think of, to be able to use a.b.c is to Define b as a static member with a getter, that returns an instantiated class. This is usually helpful for Singletons, for example as used in Firebase:
class FirebaseAuth {
  // ...
  static FirebaseAuth get instance {
    FirebaseApp defaultAppInstance = Firebase.app();

    if (!_firebaseAuthInstances.containsKey(defaultAppInstance.name)) {
      _firebaseAuthInstances[defaultAppInstance.name] = FirebaseAuth._(
        app: Firebase.app(),
      );
    }

    return _firebaseAuthInstances[defaultAppInstance.name];
  }
  // ...
}

Recommendation

In your particular case I would have calcSome defined in a helpers/responsive.dart file. If you have a lot of different helpers, you can make a library out of it.

Related