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 #59778

Open
stephane-archer opened this issue Dec 20, 2024 · 1 comment
Labels
analyzer-refactoring area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)

Comments

@stephane-archer
Copy link

Originally reported: Dart-Code/Dart-Code#5374

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);
  }
}
@stephane-archer stephane-archer added the area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. label Dec 20, 2024
@bwilkerson bwilkerson added analyzer-refactoring P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior) labels Dec 20, 2024
@lrhn
Copy link
Member

lrhn commented Dec 20, 2024

I'd suggest that the result becomes

import 'package:flutter/material.dart';

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

  @override
  Widget build(BuildContext context) {
    return const ExtractedWidget();
  }
}

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

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

That is, when extracting a widget expression that contains a free variable denoting a constant value, the result should have the same constant value. That may require either inlining the expression for the constant value, or introducing a local const variable for it in the new place, if the constant variable is local and not something that can be referenced from the new location. Or maybe hoisting the declaration.

Changing the expression to not be constant is a bigger change than duplicating a constant expression (which evaluates to the same canonicalized value no matter where it occurs).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
analyzer-refactoring area-analyzer Use area-analyzer for Dart analyzer issues, including the analysis server and code completion. P2 A bug or feature request we're likely to work on type-bug Incorrect behavior (everything from a crash to more subtle misbehavior)
Projects
None yet
Development

No branches or pull requests

3 participants