Reputation: 469
I need help to understand how the _buildBody() method works.
I have two questions:
Why is the parameter in the closure (_)
??
But, why is (context)
at the end?
I can understand the Map declaration like this:
return <Key,Value> {
Key : Value /* Value is a closure */
}[index](context)
Here you have the code:
enum Page { basic, fetch, custom }
Page _page = Page.basic;
@override
Widget build(BuildContext context) {
return Scaffold(
body: _buildBody()
),
);
}
Widget _buildBody() {
return <Page, WidgetBuilder>{
Page.basic: (_) => SliversBasicPage(),
Page.fetch: (_) => NetworkingPage(),
Page.custom: (_) => ActivitiesPage.withSampleData()
}[_page](context);
Upvotes: 0
Views: 858
Reputation: 3768
Why is the parameter in the closure (_) ??
From the Dart Language tour you can tell that any parameter that starts with underscore is private to this class:
If an identifier starts with an underscore (_), it’s private to its library.
So, a declaration of a function as follows:
(_) => SliversBasicPage()
means it's a function that will receive a parameter, but it's made clear that it won't make any use of it. (Since it has no name.)
See also this answer.
But, why is (context) at the end?
For the same reason above, all of the functions in the declared map must receive an argument because they were declared this way (with the underscore). So to actually run this function and get the result of it, you need to call it with the necessary arguments.
In this case, the context
variable won't be of any use to the example functions you supplied, but it may be to a later implemented one.
Upvotes: 1