Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hooks should only be called within the build method of a widget. Error #118

Closed
DjamelAbbou opened this issue Nov 28, 2023 · 4 comments
Closed
Labels
bug Something isn't working

Comments

@DjamelAbbou
Copy link

Describe the bug
I wanted to use both functional widgets and hooks to build flutter apps, but as i tried using the useState() hook it gave me the error: Hooks should only be called within the build method of a widget.
Even tho i did add the @hwidget.

To Reproduce
Just run flutter create <some_name> and then paste the following small code:

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

part 'main.g.dart';

@hwidget
Widget homePage() {
  final counter = useState(0);

  return MaterialApp(
      home: SafeArea(
    child: Scaffold(
      body: Center(
        child: ElevatedButton(
            onPressed: () => counter.value++,
            child: Text('Click to increase value: $counter')),
      ),
    ),
  ));
}

void main() {
  runApp(MaterialApp(home: homePage()));
}

Expected behavior
I expected the code to compile and run.

@DjamelAbbou DjamelAbbou added the bug Something isn't working label Nov 28, 2023
@rrousselGit
Copy link
Owner

Don't use homePage, use HomePage

@DjamelAbbou
Copy link
Author

Thank you for answering
I am also using functional_widget package with the hook package, and when i renamed homePage to HomePage, functional_widget package gave out the following error:
The function name must start with a lowercase character.
Is there some way to disable this forcing of having to start with a lowercase character?
Or is there a specific way for using both functional_widget and flutter hooks at the same time?

@rrousselGit
Copy link
Owner

when i renamed homePage to HomePage, functional_widget package gave out the following error:

Don't rename homePage to HomePage.
functional_widget generated a class named HomePage. Use that

@DjamelAbbou
Copy link
Author

Ohhh okay i see, well my bad.
Thanks for helping out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants