Complete Flutter Project Solutions

How to keep all the constants in Flutter?

How to keep all the constants in Flutter?

Every application requires a fixed set of constants. So, in this article, we are going to see how to keep all the constants in Flutter.

How to keep all the constants in Flutter?

Developers use Constants frequently in a Flutter Mobile Application. So, sometimes it can become quite difficult for a developer to manage all the constants for easy reference. The best practice to keep all the constants is to use an InheritedWidget. Code Snippet will look like the below:

class MyConstants extends InheritedWidget {
  static MyConstants of(BuildContext context) => context. dependOnInheritedWidgetOfExactType<MyConstants>();

  const MyConstants({Widget child, Key key}): super(key: key, child: child);

  final String successMessage = 'Some message';

  @override
  bool updateShouldNotify(MyConstants oldWidget) => false;
}

Then inserted at the root of your app:

void main() {
  runApp(
    MyConstants(
      child: MyApp(),
    ),
  );
}

And used as such:

@override
Widget build(BuilContext context) {
  return Text(MyConstants.of(context).successMessage);
}

The preferred solution is to make your own Dart library. Make a new dart file named Constants. dart, and add the following code:

const String SUCCESS_MESSAGE=" You will be contacted by us very soon.";

Then add the following import statement to the top of any dart file which needs access to the constants:

import 'constants.dart' as Constants;

Note: If constants.dart is in a different directory then you will need to specify the path to constants.dart in your import statement.

You could use a relative path like the below:

import '../assets/constants.dart' as Constants;

Or an absolute path from the lib directory:

import 'package:<your_app_name>/assets/constants.dart' as Constants;

Now you can easily access your constants with this syntax:

String a = Constants.SUCCESS_MESSAGE;

For all constants, just create constants.dart file under lib folder or lib/util folder. Then keep all constant variables as follows :

const SUCCESS_MESSAGE=" You will be contacted by us very soon.";

// Api related 
const apiBaseURL = "https://baseurl.com";
   
const userLoginApi = "login";
const userSignupApi = "signup";
   
// Shared Preference keys 
const kDeviceName = "device_name";
const kDeviceUDID = "device_id";

// Asset Constants
const navBarLogoImage = "assets/images/home_images/sample.png

then import constants.dart file in the required class and use it directly.

Conclusion:

Thanks for being with us on a Flutter Journey!

So, in this article, we have seen how to keep all the constants in Flutter. Also, feel free to comment and provide any other suggestions regarding Flutter.

Flutter Agency is our portal Platform dedicated to Flutter Technology and Flutter Developers. The portal is full of cool resources from Flutter like Flutter Widget GuideFlutter ProjectsCode libs and etc.

Flutter Agency is one of the most popular online portals dedicated to Flutter Technology. Daily thousands of unique visitors come to this portal to enhance their knowledge of Flutter.

Request a Quote