learn-dart icon indicating copy to clipboard operation
learn-dart copied to clipboard

String with single or double quotes?

Open SimonLab opened this issue 5 years ago • 2 comments

You can represent a string with either single quotes or double quotes without any difference.

const hello1 = "hello";
const hello2 = 'hello';
hello1 == hello2; //true

To escape specific characters in a string use \

Dart style guide let you decide which one you prefer to use, just be consistent. Flutter style guide recommend to use single quote: https://github.com/flutter/flutter/wiki/Style-guide-for-Flutter-repo#prefer-single-quotes-for-strings

ref:

  • https://stackoverflow.com/questions/54014913/difference-between-single-and-double-quotes-in-flutter-dart
  • https://dev.to/thphuc/string-in-dart-flutter-things-you-should-know-2baf

SimonLab avatar Nov 25 '20 16:11 SimonLab

@SimonLab good question, thanks for opening this issue. 👍

We should use whatever the "Effective Dart" Linter enforces. e.g: https://dart.dev/guides/language/effective-dart/usage#prefer-using-interpolation-to-compose-strings-and-values (all the examples use single quotes)

And if you really want to be strict, use the "Pedantic" linter: https://pub.dev/packages/pedantic which is what they use at Google.

See: https://medium.com/flutter-community/effective-code-in-your-flutter-app-from-the-beginning-e597444e1273

But yeah, the tl;dr is: single quotes for strings in Dart/Flutter. ✅

nelsonic avatar Nov 25 '20 17:11 nelsonic

to quote something inside string we need to use double quote 'Hello My Name is "Sirajul" ';

siraajul avatar Apr 10 '23 14:04 siraajul