1

BuildContext is show which widget in the tree so each widget has that. and also i know why builder widget need in below example

 Widget build(BuildContext context) {
    return new Scaffold(
      appBar: new AppBar(
        title: new Text(widget.title),
      ),
      body: new Container(),
      /// Builders let you pass context
      /// from your *current* build method
      /// Directly to children returned in this build method
      ///
      /// The 'builder' property accepts a callback
      /// which can be treated exactly as a 'build' method on any
      /// widget
      floatingActionButton: new Builder(builder: (BuildContext context) {
        return new FloatingActionButton(onPressed: () {
          Scaffold.of(context).showSnackBar(
                new SnackBar(
                  backgroundColor: Colors.blue,
                  content: new Text('SnackBar'),
                ),
              );
        });
      }),
    );
  }

Beacuse in floationactionbutton it has to know scaffold. if i don't use builder that widget use context come from build function and that's not contain scaffold info here is question some cases i saw hyphen is used in context space

builder: (_) {}

like this

return Consumer<CounterProvider>
( builder: (_, counter, child) => Scaffold( appBar: AppBar( title: Text(widget.title), ),

or like this how is it work? why context don't need?

evergreen
  • 681
  • 10
  • 22

1 Answers1

1

You can use an underscore (not a hyphen) instead of specifying the parameter name if you don't use the value passed in to the parameter.

In your Consumer example, an underscore is used because BuildContext is not used in the builder function; you don't need it to instantiate Scaffold, AppBar and Text. On the other hand, you need one in order to call Scaffold.of(context).showSnackBar().

In the same manner, you can replace counter and child with __ and ___ (double and triple underscores respectively) as they are not used as well. Note you cannot use a single underscore for each of them as it is already used in place of context.


Example of usage of BuildContext in Consumer

You can think that it works in the same way as in the Builder class.

Scaffold(
  body: ChangeNotifierProvider<Foo>(
    create: (context1) => Foo(),
    child: Consumer<Foo>(
      builder: (context2, foo, child) {
        return RaisedButton(
          child: Text(foo.message),
          onPressed: () {
            // You can get ScaffoldState using context2
            // which is impossible with context1
            Scaffold.of(context2).showSnackBar(...);
          },
        );
      },
    },
  ),
)

For more information, see here.


What is builder?

builder is an argument where you pass in a function, which is called when necessary with some values passed in for your convenience.

A lot of widgets have builder, but to take Consumer as an example, it obtains some value given by Provider() somewhere up the tree, and then calls the builder function with values including the obtained one in order to build a widget tree using the widget returned from the function.

In your example, Consumer obtains the counter value from an ancestor and calls the builder function, passing in BuildContext, the counter value (obtained from an ancestor), and the child widget (which is null if you have omitted the child argument of Consumer).

kaboc
  • 814
  • 1
  • 6
  • 23