Skip to content

Latest commit

 

History

History
51 lines (37 loc) · 1.68 KB

9. Converting Functions And Buttons.md

File metadata and controls

51 lines (37 loc) · 1.68 KB

--> Converting Function And Buttons.

    void selectedAnswer(){
        print('Answer Chosen');
    }

    ...
        onPressed:selectedAnswers(), #Error
    ...

    ...
        onPressed:selectedAnswers, #NoError
    ...

-- onPressed() takes a function so one thing we can do that we can create a seprate function and wire that inside onPressed. we can create function outside of our class but thats a bad idea. because classes always work standalone, so everything thats belong to a widget should go into the same class, all the data widget uses and so on. widget is a standalone unit.

    ...
        onPressed:selectedAnswers(), #Error
    ...

-- here if we pass the function with paranthesis it will automatically get executed on when dart build our app causing error but we want to trigger it ourself.

    ...
        oonPressed:selectedAnswers, #NoError
    ...

-- So We pass the pointer of function not function itself. so we are telling the onPressed its the name of the function which you should execute when user pressed the button. so we are passing the name of the function to onPressed and not the result of the function.

--> Anonymous Function

    void selectedAnswer(){

    } #Named-Function

-- we can also use anonymous function instead of named-function,

    ...
        onPressed : () => Print('Answer Chosen') #Anonymous Function
    ...

-- now if u have a function which you only need on specific place only and you are never calling it from anywhere else. you can also use anonymous function. please not that its not get called automatically when flutter build our app because its just function defination and we didnt used () too.