How to call a Variable inside String in Flutter

In some scenarios, you may want to get an existing variable inside your string. Hence, let’s check how to call variables inside a string in Flutter.

Calling a variable inside string is extremely easy in Flutter. You just need to use the dollar syntax as given below.

var text = 'Flutter $value Example';

Here $value represents an already defined variable named value.

Still unclear? See the complete Flutter example given below.

import 'package:flutter/material.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  final String text = 'world!';
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Welcome to Flutter',
      home: Scaffold(
        appBar: AppBar(
          title: Text('Welcome to Flutter'),
        ),
        body: Center(
          child: Text('hello $text'),
        ),
      ),
    );
  }
}

The above example will show you a text with value hello world!.

Leave a Comment

Your email address will not be published. Required fields are marked *