Lost with callbacks in Chapter 6.9

In chapter 6.9, I’m getting lost … probably because of my limited understanding of Dart and no real experience with OO programming … can someone help me determine how to interpret these 2 lines?

Also, at what point should I decide to stop learning Flutter and start a Dart tutorial instead and come back to Flutter afterwards?

class GroceryItemScreen extends StatefulWidget {
  // 1
  final Function(GroceryItem) onCreate;
  // 2
  final Function(GroceryItem) onUpdate;

onCreate and onUpdate are two properties. Both have type Function. That means any instance of GroceryItemScreen can call such functions.

I’d say any time you don’t understand something in Flutter is a good time to take a detour and learn some of the underlying Dart concepts.


@jefff9511 you can think of these like callback functions. Callback functions are useful when you need to know when the user/system did something

For example in this case:

  • onCreate, lets you know when the user created a new grocery item.
  • onUpdate, lets you know when the user updated an existing grocery item

These callbacks allow you to handle state changes, or perform any UI updates.

@funkyboy@jomoka … My confusion is over the (GroceryItem). Is that simply the type of data that will be operated on? In some other example, it might have been (String).

Yes treat these like functions, it could be a String, GroceryItem, or any object or type you want.

You could even have more than one parameter.

final Function(GroceryItem, String, Int, Bool) onCreate;
1 Like

@jomoka Oh … OK … I think I’ve got it.

In your last example, the onCreate parameter is a callback function in which you’re passing a GroceryItem, a String, an int and a Bool.

1 Like

Just like a property of type String we can assign a String value,
final Function(GroceryItem) onCreate; means that onCreate is a property that we can assign a function that takes GroceryItem as parameter.

1 Like

Thanks @alilosoft … that makes a LOT of sense to me.