rrousselGit/functional_widget

Cannot generate widgets

blackshot opened this issue · 5 comments

Describe the bug
the generator outputs:

[INFO] ------------------------------------------------------------------------

[INFO] Starting Build

[INFO] Updating asset graph...
[INFO] Updating asset graph completed, took 1ms

[INFO] Running build...
[SEVERE] functional_widget:functional_widget on lib/main.dart:

Invalid prototype. The function must be synchronous, top level, and return a Widget
package:testapp/main.dart:13:8
   ╷
13 │ Widget myApp(){
   │        ^^^^^
   ╵
[INFO] Running build completed, took 121ms

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 35ms

[SEVERE] Failed after 158ms

it seems that is throwing this error, but i don't see why.

if (function.isAsynchronous ||
function.isExternal ||
function.isGenerator ||
function.returnType.getDisplayString(withNullability: true) !=
'Widget') {
throw InvalidGenerationSourceError(
'Invalid prototype. The function must be synchronous, top level, and return a Widget',
element: function,
);
}

To Reproduce
I don't know, i just wrote the functional widget as the documentation says but the generator doesn't work

Expected behavior
generate main.g.dart

Extra info
main.dart (the only file is a simple counter app)

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

part 'main.g.dart';

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

@swidget
Widget myApp(){
  return MaterialApp(
    title: 'Flutter Demo',
    routes: {
      '/': (context) => MyHomePage(title: 'Rutas lmL')
    },
    theme: ThemeData(
      primarySwatch: Colors.lightGreen,
    ),
  );
}


@hwidget
Widget myHomePage({String title}){
  final countState = useState(0);

  void increment() => countState.value++;
  void decrement() => countState.value--;

  return Scaffold(
    appBar: AppBar(
      title: Text(title),
    ),
    body: Column(
      mainAxisAlignment: MainAxisAlignment.center,
      crossAxisAlignment: CrossAxisAlignment.stretch,
      children: [
        Text('Presionaste ${countState.value} veces', textAlign: TextAlign.center,),
        TextButton.icon(onPressed: increment, label: Text('Incrementar'), icon: Icon(Icons.add)),
        TextButton.icon(onPressed: decrement, label: Text('Decrementar'), icon: Icon(Icons.vertical_align_bottom))
      ],
    ),
  );
}

Dart version: Dart SDK version: 2.12.2 (stable) (Wed Mar 17 10:30:20 2021 +0100) on "windows_x64"
Flutter doctor

Doctor summary (to see all details, run flutter doctor -v):
[✓] Flutter (Channel stable, 2.0.4, on Microsoft Windows [Version 10.0.21313.1000], locale es-CL)
[✓] Android toolchain - develop for Android devices (Android SDK version 30.0.3)
[✓] Chrome - develop for the web
[✓] Android Studio (version 4.1.0)
[✓] IntelliJ IDEA Ultimate Edition (version 2019.3)
[✓] VS Code (version 1.55.1)
[✓] Connected device (3 available)

pubspec

name: testapp
description: A new Flutter project.

# The following line prevents the package from being accidentally published to
# pub.dev using `pub publish`. This is preferred for private packages.
publish_to: 'none' # Remove this line if you wish to publish to pub.dev

# The following defines the version and build number for your application.
# A version number is three numbers separated by dots, like 1.2.43
# followed by an optional build number separated by a +.
# Both the version and the builder number may be overridden in flutter
# build by specifying --build-name and --build-number, respectively.
# In Android, build-name is used as versionName while build-number used as versionCode.
# Read more about Android versioning at https://developer.android.com/studio/publish/versioning
# In iOS, build-name is used as CFBundleShortVersionString while build-number used as CFBundleVersion.
# Read more about iOS versioning at
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
version: 1.0.0+1

environment:
  sdk: ">=2.7.0 <3.0.0"

dependencies:
  flutter:
    sdk: flutter
  cupertino_icons: ^1.0.2
  flutter_hooks: ^0.16.0
  functional_widget_annotation: ^0.9.0

dev_dependencies:
  flutter_test:
    sdk: flutter
  functional_widget: ^0.9.0
  build_runner: ^1.9.0
# For information on the generic Dart part of this file, see the
# following page: https://dart.dev/tools/pub/pubspec

# The following section is specific to Flutter.
flutter:

  # The following line ensures that the Material Icons font is
  # included with your application, so that you can use the icons in
  # the material Icons class.
  uses-material-design: true

  # To add assets to your application, add an assets section, like this:
  # assets:
  #   - images/a_dot_burr.jpeg
  #   - images/a_dot_ham.jpeg

  # An image asset can refer to one or more resolution-specific "variants", see
  # https://flutter.dev/assets-and-images/#resolution-aware.

  # For details regarding adding assets from package dependencies, see
  # https://flutter.dev/assets-and-images/#from-packages

  # To add custom fonts to your application, add a fonts section here,
  # in this "flutter" section. Each entry in this list should have a
  # "family" key with the font family name, and a "fonts" key with a
  # list giving the asset and other descriptors for the font. For
  # example:
  # fonts:
  #   - family: Schyler
  #     fonts:
  #       - asset: fonts/Schyler-Regular.ttf
  #       - asset: fonts/Schyler-Italic.ttf
  #         style: italic
  #   - family: Trajan Pro
  #     fonts:
  #       - asset: fonts/TrajanPro.ttf
  #       - asset: fonts/TrajanPro_Bold.ttf
  #         weight: 700
  #
  # For details regarding fonts from package dependencies,
  # see https://flutter.dev/custom-fonts/#from-packages

I just solved it changing flutter create's default sdk environment in pubspec.yaml from

environment:
  sdk: ">=2.7.0 <3.0.0"

to

environment:
  sdk: ^2.12.0

As shown in the example from 'functional_widget package.

This error is not documented and i don't understand why happened so i cannot document it. it would be nice if @rrousselGit could do it.

Sorry for the late reply

Honestly I don't quite know how you ended up with this error. It may have been caused by a bugged version of a transitive dependency

hxse commented

I tested the same code, there is no way to run.
image

I tested the same code, there is no way to run. image

thats weird because i ran it just changing the sdk version.

I'll close this since it was resolved