Leave a rating/review
It’s time to tackle the next item on our programming to-do list: “Read the value of the slider after the user presses the Hit Me button.”
Before we start working on this, I want to tell you about a very important data type you’ll use in your Flutter apps: Strings.
To create a String in Dart, you simply surround some text by single quotes. If you are coming from other languages, you’ll be used to working with double quotes but single quotes is a Flutter style convention.
Behind the scenes, strings are just a sequence of characters. You can imagine them as a bunch of characters hanging on a piece of string, like you see here.
Strings in Dart have a cool feature called string interpolation. That’s just a fancy way of saying that you can put placeholder values inside your string, that are replaced dynamically by code when the app runs.
Imagine you have a string where you want to put a dynamic value inside at runtime. For example, maybe you want to say Hello, and then the name of the user of the app.
To do this, wherever you want the value to appear in your string, you put a dollar sign in front of the value inside the string.
In this example, if name is set to Joe, at runtime the string will become “Hey, Joe”, a famous song by Jimi Hendrix.
You can also evaluate expressions when performing string interpolation. You just wrap the expression inside curly braces and put a dollar sign in from the curly braces.
Let’s try this out by making our app print out the value of the slider, inside a string.
Open your project in progress or download the starter project for this episode. In main.dart, update the content of the alert
return AlertDialog(
title: const Text('Hello there!'),
content: Text('The slider\'s value is ${_model.current}'),
Now build an run. Move the slider, and you’ll get the result. We have the skeleton of our app up and running but believing it or not, even this course is ending, we are just getting started. See you in the wrap up to see where to go from here.