Home > Blockchain >  What is the difference between function() and ()=> function() in flutter?
What is the difference between function() and ()=> function() in flutter?

Time:02-17

I have met a problem which is "Exception has occurred. FlutterError (setState() or markNeedsBuild() called during build.".

I solve the problem with the following code:

  @override
  Widget build(BuildContext context) {
    return GestureDetector(
      //onTap: press(),  error
      onTap: () => press()

But I don't know why.

So, I want to know What is the difference between function() and ()=> function() in flutter?

CodePudding user response:

  onTap: () => press() 
   onTap: (){press();} 

are the same but (lambda)=> is used for one line code just short syntex

CodePudding user response:

there is no difference between ()=> function and function.

but there is a difference between how you need to call your function.

the first way is this way:

onTap: Press,

and the other way:

onTap: () => Press();

well you can get the {} from the function if you call they so: function();

and you can get () {} from the function if you only call the name: function

  • Related