Singletons can be used in various development platforms to define global parameters and store data throughout the lifecycle. So how should singletons be used in Flutter? The blogger did a search, and most of what he found was something like this:

class Manager {
  // Factory mode
  factory Manager() =>_getInstance()
  static Manager get instance => _getInstance();
  static Manager _instance;
  Manager._internal() {
    / / initialization
  }
  static Manager _getInstance() {
    if (_instance == null) {
      _instance = new Manager._internal();
    }
    return_instance; }}Copy the code

For starters, after watching this, do you know how to define parameters? Anyway, when the blogger first learned it, he looked confused and didn’t know how to define parameters and how to value them.

Next, the blogger takes you through the use of singletons and other extensions that meet the needs of most situations. First, paste the code, not much, let’s take a look:


abstract class _Glb {
  ///appKey
  String get appKey;

  /// Location information
  String get location;

  /// User name
  String get userName;

  / / / user id
  String get userID;

  /// Domain address
  String get baseURL;
}

class Glb extends _Glb {
  _Glb proxy;

  /// Differentiate different environments according to the configuration
  Glb._internal() {
    if (1= =1) {
      proxy = _TestGlb();
    } else if (1= =1) {
      proxy = _DevGlb();
    } else if (1= =1) {
      proxy = _ProGlb();
    } else{ proxy = _DefaultGlb(); }}static Glb _singleton = Glb._internal();

  factory Glb() => _singleton;

  @override
  String get appKey => proxy.appKey;

  @override
  String get location => proxy.location;

  @override
  String get userName => proxy.userName;

  @override
  String get userID => proxy.userID;

  @override
  String get baseURL => proxy.baseURL;
}

class _DefaultGlb extends _Glb {
  @override
  String get appKey => "The default Appkey";

  @override
  String get location => "Default address";

  @override
  String get userName => "Default nickname";

  @override
  String get userID => "Default user ID";

  @override
  String get baseURL => "The default URL";
}

/// Production environment parameters
class _ProGlb extends _DefaultGlb {
  @override
  String get baseURL {
    // get the corresponding baseURL based on the environment
    return 'xxxxxx';
  }

  @override
  String get appKey {
    /// Obtain the corresponding appKey based on the environment
    return 'xxxxxxx'; }}/// Development server parameters
class _DevGlb extends _DefaultGlb {
  @override
  String get baseURL {
    // get the corresponding baseURL based on the environment
    return 'xxxxxx';
  }

  @override
  String get appKey {
    /// Obtain the corresponding appKey based on the environment
    return 'xxxxxxx'; }}/// Test server parameters
class _TestGlb extends _DefaultGlb {
  @override
  String get appKey {
    /// Obtain the corresponding appKey based on the environment
    return 'xxxxxxx';
  }

  @override
  String get location => "xx";

  @override
  String get userName {
    return 'CodingFire';
  }

  @override
  String get userID {
    return '0001';
  }

  @override
  String get baseURL {
    // get the corresponding baseURL based on the environment
    return 'xxxxxx'; }}Copy the code

If you look closely, you’ll notice some similarities with the code posted at the top. Yes, it’s all singleton declarations and initializations, but there’s nothing in the code at the top that tells you how to define parameters and how to use them. Here, the blogger gives a brief description of the structure:

  • Methods modified by abstract methods to be inherited by other methods, which then have these parameters.
abstract class _Glb {
  ///appKey
  String get appKey;

  /// Location information
  String get location;

  /// User name
  String get userName;

  / / / user id
  String get userID;

  /// Domain address
  String get baseURL;
}
Copy the code
  • Singleton initialization, much the same, here can be based on the environment variable to distinguish between different environments, at the same time here to implement abstract method get method, otherwise will report an error.
class Glb extends _Glb {
  _Glb proxy;

  /// Differentiate different environments according to the configuration
  Glb._internal() {
    if (1= =1) {
      proxy = _TestGlb();
    } else if (1= =1) {
      proxy = _DevGlb();
    } else if (1= =1) {
      proxy = _ProGlb();
    } else{ proxy = _DefaultGlb(); }}static Glb _singleton = Glb._internal();

  factory Glb() => _singleton;

  @override
  String get appKey => proxy.appKey;

  @override
  String get location => proxy.location;

  @override
  String get userName => proxy.userName;

  @override
  String get userID => proxy.userID;

  @override
  String get baseURL => proxy.baseURL;
}
Copy the code
  • Here is a method that inherits Glb’s default configuration parameters:
class _DefaultGlb extends _Glb {
  @override
  String get appKey => "The default Appkey";

  @override
  String get location => "Default address";

  @override
  String get userName => "Default nickname";

  @override
  String get userID => "Default user ID";

  @override
  String get baseURL => "The default URL";
}
Copy the code
  • Then, the configuration parameters of other environments are inherited from the default method to prevent empty data. After inheritance, as long as the corresponding GET method is implemented, return parameter will be used in the corresponding configuration:
/// Production environment parameters
class _ProGlb extends _DefaultGlb {
  @override
  String get baseURL {
    // get the corresponding baseURL based on the environment
    return 'xxxxxx';
  }

  @override
  String get appKey {
    /// Obtain the corresponding appKey based on the environment
    return 'xxxxxxx'; }}/// Development server parameters
class _DevGlb extends _DefaultGlb {
  @override
  String get baseURL {
    // get the corresponding baseURL based on the environment
    return 'xxxxxx';
  }

  @override
  String get appKey {
    /// Obtain the corresponding appKey based on the environment
    return 'xxxxxxx'; }}/// Test server parameters
class _TestGlb extends _DefaultGlb {
  @override
  String get appKey {
    /// Obtain the corresponding appKey based on the environment
    return 'xxxxxxx';
  }

  @override
  String get location => "xx";

  @override
  String get userName {
    return 'CodingFire';
  }

  @override
  String get userID {
    return '0001';
  }

  @override
  String get baseURL {
    // get the corresponding baseURL based on the environment
    return 'xxxxxx';
  }
Copy the code
  • How do I use the value? I used it yesterday in another blog about layout, and posted it again:
// if you want to use userName, you need to import this file first
Glb().userName
Copy the code

Have you learned? Go and try it! Demo Download Address