0

I've written extension methods on lots of dart stuff, but when I tried to do it FontWeight from dart:ui it isn't recognized as legit.

extension FontWeightExtension on FontWeight {
  static const FontWeight thin = FontWeight.w100;
  static const FontWeight extraLight = FontWeight.w200;
  static const FontWeight light = FontWeight.w300;
  static const FontWeight regular = FontWeight.w400;
  static const FontWeight medium = FontWeight.w500;
  static const FontWeight semiBold = FontWeight.w600;
  static const FontWeight extraBold = FontWeight.w800;
  static const FontWeight thick = FontWeight.w900;
}

then

import 'package:my_package/utils/extensions.dart';
FontWeight.medium...

the linter gives me this error:

The getter 'medium' isn't defined for the type 'FontWeight'. Try importing the library that defines 'medium', correcting the name to the name of an existing getter, or defining a getter or field named 'medium'.

So I thought the static thing was messing it up so I tried it without static and got the same issue.

But check this out:

FontWeight().medium

now I get two errors:

The class 'FontWeight' doesn't have a default constructor. Try using one of the named constructors defined in 'FontWeight'

Of course, but I also get this:

Static field 'medium' can't be accessed through an instance. Try using the class 'FontWeightExtension' to access the field.

So it knows about the extension method, but I can't use it on FontWeight

Is there anyway to get around this? How do you write an extension method on a class that doesn't have a constructor?

MetaStack
  • 3,266
  • 4
  • 30
  • 67
  • 1
    It has nothing to do with the constructor, but that you cannot add static members to classes using extension methods. Static members of extension methods can be called using the name of the extension and is therefore static to the extension itself (so useful if you want to have some helper methods for your extension). – julemand101 Jan 05 '22 at 18:49
  • @julemand101 So there's no way to add a static method to a class? And if I want to (I don't really want to) add these methods as non-static getters on the class I can't access them (by instantiating the object) because it doesn't have a constructor, so there's really no way to add any method to a non-constructored class... right? – MetaStack Jan 05 '22 at 18:53
  • 2
    Correct. As it is with latest version of Dart, you cannot add new static members to classes without modifying the class itself (which is not really a possibility when using third party libraries). I would then say that adding static members to third party classes is really not that useful in lot of cases and the "workaround" is to make your own class in your project with the static members you want. – julemand101 Jan 05 '22 at 18:56
  • 2
    There is a feature request for this on the dart github repo https://github.com/dart-lang/language/issues/723 . It might interest you to have a look at the discussion. – mmcdon20 Jan 05 '22 at 19:00

0 Answers0