Skip to content

feat: add #[frb(sync)] to RustState::new for synchronous initialization#3003

Open
guoyu07 wants to merge 1 commit intofzyzcjy:masterfrom
guoyu07:rust_ui_counter
Open

feat: add #[frb(sync)] to RustState::new for synchronous initialization#3003
guoyu07 wants to merge 1 commit intofzyzcjy:masterfrom
guoyu07:rust_ui_counter

Conversation

@guoyu07
Copy link
Copy Markdown

@guoyu07 guoyu07 commented Mar 3, 2026

Fix “tearoff_of_generative_constructor_of_abstract_class” in rust_ui_counter

Changes

Fixes error in frb_example/rust_ui_counter/ui/lib/main.dart

void main() => runRustApp(body: body, state: RustState.new);

Details about the 'tearoff_of_generative_constructor_of_abstract_class' diagnostic produced by the Dart analyzer.

A generative constructor of an abstract class can't be torn off.
Try tearing off a constructor of a concrete class, or a non-generative constructor.
abstract class RustState implements RustOpaqueInterface

To resolve this, when added this #[frb(sync)] attribute to pub fn new() -> Self, the generated wrapper class in Dart, allows us to use its constructor tear‑off safely.

Version

  • flutter_rust_bridge: 2.11.1
  • rust: 1.93.1
  • dart: Dart SDK version: 3.11.0 (stable)
  • flutter: 3.41.2 (channel stable)
  • os: MacOS Tahoe 26.0.1

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request addresses a Dart analyzer diagnostic that occurred when attempting to tear off a generative constructor of an abstract class, specifically RustState.new, which is generated by flutter_rust_bridge. By introducing a #[frb(sync)] attribute to the Rust RustState::new function, the generated Dart code now correctly supports constructor tear-offs, allowing for a more idiomatic and error-free initialization pattern in the Dart UI.

Highlights

  • Dart Analyzer Error Resolution: Resolved the "tearoff_of_generative_constructor_of_abstract_class" diagnostic in Dart by modifying the Rust code, which previously prevented safe constructor tear-offs for the generated RustState class.
  • Synchronous Initialization Attribute: Added the #[frb(sync)] attribute to the RustState::new function in frb_example/rust_ui_counter/src/app.rs. This change enables the generated Dart wrapper class to safely use its constructor tear-off, aligning with Dart's requirements for abstract class constructors.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • frb_example/rust_ui_counter/src/app.rs
    • Added #[frb(sync)] attribute to pub fn new() -> Self.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request introduces the #[frb(sync)] attribute to the RustState::new function. This change correctly configures the code generator to produce a synchronous Dart function for state initialization, resolving a tearoff_of_generative_constructor_of_abstract_class error in the Dart UI code. The change is appropriate and effectively addresses the issue.

@codecov
Copy link
Copy Markdown

codecov Bot commented Mar 3, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 98.57%. Comparing base (172230f) to head (b60d2c3).

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #3003   +/-   ##
=======================================
  Coverage   98.57%   98.57%           
=======================================
  Files         464      464           
  Lines       19202    19202           
=======================================
  Hits        18928    18928           
  Misses        274      274           

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@fzyzcjy
Copy link
Copy Markdown
Owner

fzyzcjy commented Mar 5, 2026

Great job! But a bit confused:

it seems it is already sync by default. could you please check a bit about your config yaml

@guoyu07
Copy link
Copy Markdown
Author

guoyu07 commented Mar 5, 2026

default_dart_async: false

My config is the default flutter_rust_bridge.yaml:

rust_input: crate::api
rust_root: rust/
dart_output: lib/src/rust

I've tried to add this option with true and false, this error still exits.
frb_issue1

@fzyzcjy
Copy link
Copy Markdown
Owner

fzyzcjy commented Mar 6, 2026

(replied in #3004)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants