Mastering String Case Conversion in Dart and Flutter with Recase

Mastering String Case Conversion in Dart and Flutter with Recase

ยท

3 min read

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
๐Ÿ’
This will add 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?

๐Ÿš€
Follow me on X
ย