Mastering String Case Conversion in Dart and Flutter with Recase
Welcome 👋.
For String case conversion while Dart offer only
UPPER CASE
lower case
There is an awesome package named Recase offer additional 10 string cases. These are
snake_case
dot.case
path/case
param-case
PascalCase
Header-Case
Title Case
camelCase
Sentence case
CONSTANT_CASE
Let’s install Recase to start using any case mentioned above.
🔰Installing Recase
For Dart Project, run this command
dart pub add recase
For Flutter Project, run this command
flutter pub add recase
recase:^4.1.0
in your pubspec.yaml
file under dependencies section.Now, In your code, import recase by adding this line.
import 'package:recase/recase.dart';
Awesome!🥳 We are ready to explore the power of Recase.
🔰 Uses of Recase
Let's try all of the case conversion from Recase. There are two way to use Recase package.
Default Method
Extension Method
We'll focus on extension method because it is shorter, handy and productive to work with. We'll also demonstrate the Default method also.
Default Method
import 'package:recase/recase.dart';
void main() {
// Declare an example string
String exampleText = 'We are learning Recase package';
// Create an object.
final recase = ReCase(exampleText);
// Convert to snake_case
print(recase.snakeCase); //Prints we_are_learning_recase_package
}
Prefer video tutorial? I’ll create video soon. Stay subscribed!
Extension Method
Here we will not create any recase object. We will apply extension method directly in string.
import 'package:recase/recase.dart';
void main() {
// Declare an example string
String exampleText = 'We are learning Recase package.';
// Let's convert to dotCase
print(exampleText.dotCase); //Prints we.are.learning.recase.package
}
We can also try additional case conversion. Here is the examples
// Convert to snake_case
print(exampleText.snakeCase); //Prints we_are_learning_recase_package
// Convert to pathCase
print(exampleText.pathCase); //Prints we/are/learning/recase/package
// Convert to paramCase
print(exampleText.paramCase); //Prints we-are-learning-recase-package
// Convert to pascalCase
print(exampleText.pascalCase); //Prints WeAreLearningRecasePackage
// Convert to headerCase
print(exampleText.headerCase); //Prints We-Are-Learning-Recase-Package
// Convert to titleCase
print(exampleText.titleCase); //Prints We Are Learning Recase Package
// Convert to camelCase
print(exampleText.camelCase); //Prints weAreLearningRecasePackage
// Convert to sentenceCase
print(exampleText.sentenceCase); //Prints We are learning recase package
// Convert to constantCase
print(exampleText.constantCase); //Prints WE_ARE_LEARNING_RECASE_PACKAGE
😊 Fun facts about Recase
Keith Elliott wrote this package
Recase has zero dependency.
It's null safe from version 4.0.0.
It was maintained since 2017.
About 388 another package also depends on this Package
340 developer 👍 this package.
//At the time of writing this article
A vast amount of developer (including me) benefited from this package. My HabitLab app also depends on this awesome package.
When you build something with Recase let me know in the comments.
Its published under BSD-2-Clause License
🔰 Last Words
Finally let's 👍 like this package on pub.dev to encourage the developer. We can also ⭐️ this package on Github.
Share thoughts about this article on comments. You can also advice me, How can I improve this article even better?
Subscribe to my newsletter
Read articles from MD. ABDULLAH directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by