Example of Find and Replace Substring of String in Flutter Dart

Hello friends, In today’s tutorial we would learn about a Dart string replacing function. In flutter Using the String.replaceAll() method we can find and replace substring of string in flutter dart, replace single string character or words present in a single document. In this tutorial we are calling the String.replaceAll() method on button click event so all the changes on the string will be on application runtime. So let’s get started 🙂 .

Contents in this project Example of Find and Replace Substring of String in Flutter Dart:-

1. Open your project’s main.dart file and import material.dart package.

2. Creating void main runApp() method and here we would call our main MyApp class.

3. Creating our main MyApp extends StatelessWidget class.

4. Creating a String variable named as message with some random string. We would replace the website word in given string.

5. Creating a function named as changeData(). In this function we would use the String.replaceAll() method to find and replace Website word word with Blog word in given string. After changing we would store the result in temp string variable and then print the temp on Terminal screen.

6. Creating Widget Build Area -> Scaffold widget -> Material App -> Scaffold widget -> Container Widget -> Center Widget -> Raised Button widget. We would call the changeData function on button onPress event.

7. Complete source code for main.dart file:-


Leave a Reply

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