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

Invalid constant value error when extracting a widget with a const TextStyle parameter #5374

Closed
stephane-archer opened this issue Dec 19, 2024 · 4 comments
Labels

Comments

@stephane-archer
Copy link

stephane-archer commented Dec 19, 2024

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    const TextStyle h1 = TextStyle(color: Colors.red);
    return const Text("hello world", style: h1);
  }
}

extract widget Text("hello world", style: h1);

result:

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    const TextStyle h1 = TextStyle(color: Colors.red);
    return ExtractedWidget(h1: h1);
  }
}

class ExtractedWidget extends StatelessWidget {
  const ExtractedWidget({
    super.key,
    required this.h1,
  });

  final TextStyle h1;

  @override
  Widget build(BuildContext context) {
    return const Text("hello world", style: h1);
  }
}

There is an error regarding h1 Invalid constant value.

expected result

Here is the correct code (the const keyword is not at the right place):

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    const TextStyle h1 = TextStyle(color: Colors.red);
    return const ExtractedWidget(h1: h1);
  }
}

class ExtractedWidget extends StatelessWidget {
  const ExtractedWidget({
    super.key,
    required this.h1,
  });

  final TextStyle h1;

  @override
  Widget build(BuildContext context) {
    return Text("hello world", style: h1);
  }
}
@FMorschel
Copy link
Contributor

Just to be clear @stephane-archer, these errors with refactors/assists/fixes are all under the SDK analyzer/analysis server or similar (commenting to you since I've seen some issues you opened there). We probably can fix them here but it'll still only come to the users in a new SDK release.

@stephane-archer
Copy link
Author

I didn’t realize that the Dart SDK would be a more appropriate place to report this kind of issue. Would you like me to move it there instead?

As for your point:

but it'll still only come to the users in a new SDK release.

No worries!

@FMorschel
Copy link
Contributor

Would you like me to move it there instead?

I'd say it's up to you or @DanTup which I don't think would mind having it here. But there are always more eyes there to possibly take a try on the fix.

If you move it, you can (and I'd suggest) always link to this one so we know where it came from and people who end up here know where this conversation moved to.

@DanTup
Copy link
Member

DanTup commented Jan 7, 2025

Sorry for not responding sooner. Most of the language functionality comes from the Dart language server which lives in the SDK (generally anything that requires parsing Dart code will live there, which means the functionality is also shared across IDEs).

It's not always obvious what functionality comes from where so filing issues here is not a problem. If they're trivial fixes I can do quickly, I'll often just do them and reference the issue here - but for other things it's better to have them in SDK (where they'll have more visibility and may be fixed by others).

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants