I know the syntax for calling a function after onPressed
and onTap
for a widget. There are two options We can use either the () => function()
or the () { function(); }
syntax. What do the empty parentheses mean?
相关问题
- What means in Dart static type and why it differs
- Flutter : Prepare list data from http request
- How to schedule an alarm on specific time in Flutt
- MappedListIterable is not a SubType
- 'firebase_messaging/FirebaseMessagingPlugin.h&
相关文章
- Observatory server failed to start - Fails to crea
- Flutter error retrieving device properties for ro.
- Adding Shadows at the bottom of a container in flu
- Flutter. Check if a file exists before loading it
- Flutter - http.get fails on macos build target: Co
- Receive share file intents with Flutter
- Do stateless widgets dispose on their own?
- How to clean your build with Flutter RP2 in Androi
If I understand your question correctly, you are asking about the bolded one () => function().
With that assumption I am trying to answer.
onTap, onPressed are the taking
function
as arguments. Possible values can beI've learned to treat the empty parentheses as a build first then with the returned value execute this function.
I ran into an issue using the second method you posted where flutter would crash stating "cannot build because the framework is already building" and found this post on StackOverflow which may give you a better idea of what it means. Flutter - Cannot build because the frawework is already building
=> (fat arrow) syntax is handy for functions that contain a single statement. This syntax is especially useful when passing anonymous functions as arguments.
() => expression
or() { statements }
creates a closure or inline function.This way you create inline a function that is passed as argument to be called in case of the event
onPressed
by the widget you pass it to.The
expression
orstatements
have the context where they were created available and can access and use all members and identifiers available in that context (variables, methods, functions, typedefs, ...).If you use
onPressed: myFunction
a reference to an existing function is passed.This only works if the parameters of the callback expected by
onPressed
andmyFunction
are compatible.onPressed: myFunction()
myFunction()
is executed and the returned result is passed toonPressed
. This is a common mistake when done unintentionally when actually the intention was to pass a reference tomyFunction
instead of calling it.They are not the same thing. According the the language docs, the fat arrow is syntactical sugar for a return statement.
https://www.dartlang.org/guides/language/language-tour#functions
is comparable to this line
not this statement
I guess you got away with it because both handlers have a tendency to be void.
https://docs.flutter.io/flutter/dart-ui/VoidCallback.html
https://docs.flutter.io/flutter/gestures/GestureTapCallback.html
https://docs.flutter.io/flutter/material/ListTile/onTap.html
https://docs.flutter.io/flutter/material/IconButton/onPressed.html