Dart/Flutter - Using StringBuffer Examples

If you're using Dart and you need to perform incremental building of a string, there is a better way instead of concatenating a string with another string multiple times. You can use StringBuffer, a class for concatenating strings efficiently. The content will be stored in a buffer first before converted to a string.

Using StringBuffer in Dart/Flutter

Creating StringBuffer

Below is the constructor for creating a new StringBuffer. You can pass an optional argument content whose value will be set as the initial content.

  StringBuffer([Object content = ""])

Examples:

  StringBuffer sb = new StringBuffer();
  StringBuffer sb = new StringBuffer("Woolha");

 

Writing Values to StringBuffer in Dart/Flutter

There are some methods for adding contents to the buffer. The first one accepts any type of Object. The value will be converted to a String before being added to the buffer. Beware that null value will be converted to a non-empty String whose value is 'null'.

  void write(Object obj);

Example:

  sb.write('dart');

 

There is also a method that allows you to pass the char code.

  void writeCharCode(int charCode);

Example:

  sb.write(46); // 46 is char code of dot (.)

 

If you have an Iterable, you can add all elements in the iterable to the buffer using writeAll method. You can also specify the separator to add between elements.

  void writeAll(Iterable objects, [String separator = ""]);

Example:

  sb.writeAll({'Dart', 'Flutter'}, ',');

 

If you want to write a newline or write a value with a newline at the end, you can use writeln.

  void writeln([Object obj = ""]);

Examples:

  sb.writeln();
  sb.writeln('Dart');

 

Clearing StringBuffer in Dart/Flutter

To clear the buffer, you can use .clear() method. It will remove all values in the buffer.

  void clear()

Example:

  sb.clear();

 

Checking Length and Emptiness of StringBuffer in Dart/Flutter

If you need to get the current number of characters in the buffer, you can access the length property. To check the emptiness, you can use isEmpty property whose value is true if the current number of characters in the buffer is 0. There is also the opposite property isNotEmpty whose value is true in the opposite condition.

  int get length;
  isEmpty => length == 0;
  isNotEmpty => !isEmpty;

Examples:

  sb.length;
  sb.isEmpty;
  sb.isNotEmpty;

 

Converting StringBuffer to String in Dart/Flutter

To convert the content of the buffer to a string, use .toString() method.

  String toString();

Example:

  sb.toString();

 

StringBuffer Usage Examples

Below is some usage examples of StringBuffer.

  void main() {
    StringBuffer sb = new StringBuffer('Website: ');
  
    sb.write('woolha');
    sb.writeCharCode(46);
    sb.writeln('com');
    sb.write('Tags: ');
    sb.writeAll({'Dart', 'Flutter'}, ',');
  
    print('length: ${sb.length}');
    print('isEmpty: ${sb.isEmpty}');
    print('isNotEmpty: ${sb.isNotEmpty}');
    print('----------\n${sb.toString()}\n----------');
  
    print('CLEAR THE BUFFER');
    sb.clear();
  
    print('length: ${sb.length}');
    print('isEmpty: ${sb.isEmpty}');
    print('isNotEmpty: ${sb.isNotEmpty}');
    print('----------\n${sb.toString()}\n----------');
  }
  

Output:

  length: 38
  isEmpty: false
  isNotEmpty: true
  ----------
  Website: woolha.com
  Tags: Dart,Flutter
  ----------
  CLEAR THE BUFFER
  length: 0
  isEmpty: true
  isNotEmpty: false
  ----------
  
  ----------

 

StringBuffer Properties

  • bool isEmpty: Whether the buffer is empty.
  • bool isNotEmpty: Whether the buffer is not empty.
  • int length: Number of characters in the buffer.
  • length: The hash code for this object.
  • length: Runtime type of the object.

 

StringBuffer Methods

  • void write(Object obj): Add content of obj (converted to a string) to the buffer.
  • void writeCharCode(int charCode): Write the string representation of charCode to the buffer.
  • void writeAll(Iterable objects, [String separator = ""]): Add each iterable element to the buffer with optional separator.
  • void writeln([Object obj = ""]): Add a newline to the buffer with optional obj (converted to a string) before the newline.
  • String toString(): Returns the buffer's content as a string.
  • void clear(): Clears the buffer.