skip to Main Content

I have this method which needed a context so I’m passing it as a parameter.

Future<void> myPopup(BuildContext context) {
  return Dialogs.materialDialog(
    context: context,
    ...
  );
}

I tried to wrap the whole Dialogs.materialDialog with Builder in order for me not to pass the context anymore but I’m unable to do it.

Builder(
  builder: (BuildContext context) {
    return Dialogs.materialDialog(
      context: context,
      ...
    );
  },

How can I achieve the same output using the Builder?

2

Answers


  1. You don’t need to wrap your method in a Builder to be able to give it the context. The context can be used from the build method.

      Future<void> myPopup(BuildContext context) {
      return Dialogs.materialDialog(
        context: context,
        ...
       );
      }
      
      @override
      Widget build(BuildContext context) { //Context is already defined in the build.
        return TextButton(
          child: Text('Press me'),
          onPressed: () {
            myPopup(context);
          },
        );
      }
    

    Also @rahul has a point with the method not being assignable to the child widget as the return type is different than what is expected.

    Login or Signup to reply.
  2. not to pass the context, I think you are looking for LookupBoundary but currently it is on master-api.

    On your case it will be

    LookupBoundary(
        child: Builder(
          builder: (BuildContext context) {
            return Dialogs.materialDialog(
             context: context,
          ...
          )
        );
      },
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search