Flutter call function with parameter | Pass a function with parameters to a Callback Function | Dart

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
let's start a new flutter project let us import material.dart in main.dart void main let's call the run app function mayap class as parameter we need to create it now let's create a stateful widget let t f u l select ok give the name you can change the container and give the material app home scaffold app bar app bar title text give any title body column column children let's run add center widget and add text widget inside let's give a variable let us declare it above that variable var t x t equals sample text let's add a style to the text let's run it now has become a text display below that we can add a button widget elevated button give the child text change text an error occurred while saving unpressed function should be given now let us give null let us change it and give it a function name above we can write that function the function is to change the text when the button is clicked let us give the function name this text variable needs to be changed txt equals text changed it must be given within the set state okay let's run it the text changed when the button was clicked the text to be changed can now be passed through a parameter parameter change text let us give the parameter variable here let us give the parameter inside the unpressed function call parameter new text okay it's working now let us change the passing value okay now let's create another class and learn how to pass this function there create new dart file let's give a file name okay let us import material.dart inside the new file now let us create a stateless widget let's give the name my new class a variable for the text to be changed final string new text we can also create a function variable final function callback function now let us pass both the parameters inside the constructor this dot new text this dot callback function you can give the required keyword for both the entire code of the elevated button can be copied from the main.dart and used in the new class the parameter new text variable can be used now let's call the newly created widget on the main dot dart before that the class must be imported my new class let us give the text parameter new text callback function callback function let's name this function ok let's run it now there is an error child is mandatory in the container widget child then there is the error here we need to call the function name that we pass callback function okay you can see that the function works properly while running you
Info
Channel: True Coders
Views: 6,738
Rating: undefined out of 5
Keywords: flutter callback function with parameters, dart pass function as parameter, dart function as parameter
Id: Em4wVQY7Kj8
Channel Id: undefined
Length: 9min 47sec (587 seconds)
Published: Wed Jun 15 2022
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.