rrousselGit / functional_widget

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

Not generating any output files - probably not compatible with latest flutter / dependencies etc #113

Closed gslender closed 1 year ago

gslender commented 1 year ago

Describe the bug When using a basic example / starter app, and using the current release of functional_widget and dependencies, it doesn't produce any output files. Nothing is generated. It looks as though build_runner silently just exits without any output.

To Reproduce

import 'package:flutter/material.dart';
import 'package:functional_widget_annotation/functional_widget_annotation.dart';

part 'main.g.dart';

void main() {
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({super.key});

  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Demo',
      theme: ThemeData(
        primarySwatch: Colors.blue,
      ),
      home: const MyHomePage(title: 'Flutter Demo Home Page'),
    );
  }
}

class MyHomePage extends StatefulWidget {
  const MyHomePage({super.key, required this.title});

  final String title;

  @override
  State<MyHomePage> createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  int _counter = 0;

  void _incrementCounter() {
    setState(() {
      _counter++;
    });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text(widget.title),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            Foo(42, 22),
            const Text(
              'You have pushed the button this many times:',
            ),
            Text(
              '$_counter',
              style: Theme.of(context).textTheme.headlineMedium,
            ),
          ],
        ),
      ),
      floatingActionButton: FloatingActionButton(
        onPressed: _incrementCounter,
        tooltip: 'Increment',
        child: const Icon(Icons.add),
      ),
    );
  }

  @swidget
  Widget foo(BuildContext context, {int value = 0, int value2 = 2}) {
    return Text('$value $value2');
  }
}

And this is what the pubspec.yaml looks like

name: test_app
description: A new Flutter project.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev
version: 1.0.0+1

environment:
  sdk: '>=2.19.0 <3.0.0'

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  functional_widget_annotation: ^0.10.0

dev_dependencies:
  flutter_test:
    sdk: flutter

  flutter_lints: ^2.0.0
  functional_widget: ^0.10.1
  build_runner: ^2.3.3

flutter:
  uses-material-design: true

Expected behavior that it generates the needed main.g.dart file ??

gslender commented 1 year ago

Note that JsonSerializable on the very same setup works fine. Just adding the needed dependencies for a simple json example cause the build_runner to generate the file, but it still lacks the needed functional_widget code. So there is definitely something wrong that's specific to functional_widget

gslender commented 1 year ago

Flutter 3.7.5 • channel stable • https://github.com/flutter/flutter.git Framework • revision c07f788888 (3 days ago) • 2023-02-22 17:52:33 -0600 Engine • revision 0f359063c4 Tools • Dart 2.19.2 • DevTools 2.20.1

gslender commented 1 year ago

Also tried a @freezed example and it works fine, but also noticed you pushed the generated file into a different part with part 'main.freezed.dart' but not sure if that's something you require with functional_widget - the docs don't suggest that's needed, so just wondering

rrousselGit commented 1 year ago

Hello!

The annotation cannot be used on methods. It can only be applied on top-level functions

So:

// OK
@swidget
Widget foo(BuildContext context, {int value = 0, int value2 = 2}) {
  return Text('$value $value2');
}

clas Foo {
// Bad
  @swidget
  Widget foo(BuildContext context, {int value = 0, int value2 = 2}) {
    return Text('$value $value2');
  }
}

functional_widget doesn't even know that your method was annotated