rrousselGit / functional_widget

A code generator to write widgets as function without loosing the benefits of classes.
597 stars 46 forks source link

Extensions on BuildContext causes issues with generated widgets #58

Closed Aidanvii7 closed 4 years ago

Aidanvii7 commented 4 years ago

Steps to reproduce

  1. Create a new application with the default template flutter create example
  2. Add functional_widget and functional_widget_annotation to pubspec.yaml
  3. Ensure SDK is at least 2.6.0 to make use of extension methods
    
    environment:
    sdk: ">=2.6.0 <3.0.0"

dependencies: flutter: sdk: flutter functional_widget_annotation: ^0.5.1

builders: functional_widget: ^0.7.1

4. Create an extension on `BuildContext` (no actual extension methods are required):

extension on BuildContext {

}

5. Create a functional widget that recieves it's `BuildContext` such as:

@widget Widget example(BuildContext context) => const Placeholder();

6. Import the extension
### Expected source generated:

class Example extends StatelessWidget { const Example({Key key}) : super(key: key);

@override Widget build(BuildContext _context) => example(_context); }

### Actual source generated:

class Example extends StatelessWidget { const Example(this.context, {Key key}) : super(key: key);

final dynamic context;

@override Widget build(BuildContext _context) => example(context); }

rrousselGit commented 4 years ago

Is this still happening if you give a name to your extension?

Aidanvii7 commented 4 years ago

It is yea, I tried with a named extension first.

Aidanvii7 commented 4 years ago

There is a workaround. It generates widgets correctly if the extensions library is imported with a name.

import 'my_extension' as my_extension;

It seems extensions are still accessible without needing a library prefix, so the above will result in an unused import warning, no big deal though.

rrousselGit commented 4 years ago

Thinking about it, it's likely just because functional_widget uses an outdates analyzer

rrousselGit commented 4 years ago

This should be fixed now as analyzer has been upgraded