Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flutter: extension methods aren't recognized

I'm trying to add extension method to BorderRadius which apply rounded corner to a container the extention code:

extension on BorderRadius{
  static get r10 => const BorderRadius.all(Radius.circular(10));
}

and here is how I used it on the container:

Container(
        alignment: Alignment.center,
        width: width * 0.7,
        padding: EdgeInsets.only(top: 20, bottom: 20),
        decoration: BoxDecoration(
          borderRadius: BorderRadius.r10,
            gradient: LinearGradient(
                begin: Alignment.topLeft,
                end: Alignment.bottomRight,
                colors: [_buttonColor, Colors.purple]),
            boxShadow: const [
              BoxShadow(color: Colors.black87, blurRadius: 5)
            ]),
        child: Text(_lable))

The problem is the r10 method is not among the suggestions of the BorderRadius class and isn't recognized.

like image 820
FouadAbdelhady Avatar asked Feb 27 '26 09:02

FouadAbdelhady


1 Answers

This extension is private which means you can't use it from outside the file where you created it.

To make it global, this is what you need to do

extension BorderRadiusExt on BorderRadius {
   static get r10 => const BorderRadius.all(Radius.circular(10));
}

You can't import something that is private and be able to use it. This you have here is an anonymous extension.

To learn more refer to this article.

https://wilsonwilson.dev/articles/dart-extension-methods

like image 167
Denzel Avatar answered Mar 02 '26 01:03

Denzel