Home > Software design >  How to behave with long strings in Flutter/Dart to make code clean?
How to behave with long strings in Flutter/Dart to make code clean?

Time:01-18

I wonder how to behave with long strings to make code clean. Code example:

Text(
  'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.',
)

Is it good approach to have such a string in one line of code? It is contrary to lines_longer_than_80_chars lint. Or it is better to split it into mulitple lines like:

Text(
  'Lorem ipsum dolor sit amet, consectetur adipiscing '
  'elit, sed do eiusmod tempor incididunt ut labore et '
  'dolore magna aliqua. Ut enim ad minim veniam, quis '
  'nostrud exercitation ullamco laboris nisi ut '
  'aliquip ex ea commodo consequat. Duis aute irure '
  'reprehenderit in voluptate velit esse cillum dolore '
  'eu fugiat nulla pariatur. Excepteur sint occaecat '
  'cupidatat non proident, sunt in culpa qui officia '
  'deserunt mollit anim id est laborum.',
)

Such a splitting is time-consuming, is there any way to make it automatically, with formatter for example?

Maybe there is any better approach?

CodePudding user response:

If your string is that large, have you considered using assets?

flutter:
  assets:
    - lipsum.txt
import 'package:flutter/services.dart' show rootBundle;

Future<String> loadAsset() async {
  return await rootBundle.loadString('assets/lipsum.txt');
}

CodePudding user response:

I think using multiline apostrophe like that:

  var longString = '''
    This is very long text multiline and readable
    it's continuation. qwdoqjdoaqjshdokasaklsjdla
    sdpgksndljkfsndlfksndlfknsldkfnslkd
   ''';

It saves your time.

CodePudding user response:

First, create a Common class, and after create a variable to store this string

String description = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';

access this variable to class whenever you to use

  • Related