String

A String variable stores a sequence of characters. It's one of the most commonly used data types for storing text values like names, descriptions, messages, or any textual content.

When to Use

Use String variables when you need to:

  • Store user input from text fields

  • Display dynamic text content

  • Store API responses containing text

  • Manage labels, titles, or descriptions

  • Store formatted text or messages

Examples

Simple Text Storage

"Hello, World!"
"John Doe"
"Welcome to Digia"

Multi-line Text

"This is line one
This is line two
This is line three"

Empty String

""

Common Use Cases

1. Storing User Input

When a user types into a Text Form Field, you can store that value in a String variable:

Variable: userName (String)
Initial Value: ""

On Text Field Change:
  Set State: userName = ${currentValue}

2. Displaying Dynamic Content

Use String variables to show personalized messages:

Variable: greeting (String)
Value: "Hello, ${userName}!"

Text Widget:
  Text: ${greeting}

3. API Response Data

Store text data from API calls:

Variable: productName (String)
Value: ${apiResponse.data.name}

Working with Strings

Concatenation

Combine multiple strings together using the concat() function:

${concat(string1, " ", string2)}
${concat("Hello, ", userName, "!")}

String Interpolation

Embed variables within strings:

${concat("Welcome, ", userName, ". You have ", messageCount, " new messages.")}

String Operations

Use string operators to manipulate text. See the String Operations documentation for available functions:

  • toUpperCase() - Convert to uppercase

  • toLowerCase() - Convert to lowercase

  • trim() - Remove leading/trailing whitespace

  • substring() - Extract a portion of the string

  • split() - Split string into an array

  • And many more...

Best Practices

  • Initialize with default values: Set an initial value (even if it's an empty string "") to avoid null/undefined errors

  • Validate user input: Always validate and sanitize string input from users

  • Use concat for combining strings: Use concat() function to combine multiple strings: ${concat("Hello, ", name)}

  • Trim whitespace: Use trim() when comparing or storing user input to remove accidental spaces

  • Handle empty states: Check if a string is empty before using it in critical operations using isEmpty()

Last updated