I have created an extension in a file, when importing it to another file the compiler doesn't see it at all and says `this method isn't defined', however if I copy the extension inside the file where I need to use it works fine. I am using Android Studio.
This is the code of extension:
extension on DateTime {
String convertToString() {
DateTime date = this;
String convertedDate = date.toString();
convertedDate = convertedDate.split(' ')[0];
convertedDate =
convertedDate.split('-').reversed.toList().join('-');
return convertedDate;
}
}
Example of use: DateTime.now().convertToString();
this works only inside the same file
Also this is flutter doctor
Doctor summary (to see all details, run flutter doctor -v):
[√] Flutter (Channel stable, 3.0.1, on Microsoft Windows [Version 10.0.22000.856], locale en-US)
[√] Android toolchain - develop for Android devices (Android SDK version 32.1.0-rc1)
[√] Chrome - develop for the web
[X] Visual Studio - develop for Windows
X Visual Studio not installed; this is necessary for Windows development.
Download at https://visualstudio.microsoft.com/downloads/.
Please install the "Desktop development with C " workload, including all of its default components
[√] Android Studio (version 2021.2)
[√] VS Code (version 1.70.0)
[√] VS Code, 64-bit edition (version 1.64.1)
[√] Connected device (3 available)
[√] HTTP Host Availability
! Doctor found issues in 1 category.
CodePudding user response:
extension on DateTime
does not declare a public name for the extension, so it ends up being private to the file. From the https://dart.dev/guides/language/extension-methods:
To create a local extension that’s visible only in the library where it’s declared, either omit the extension name or give it a name that starts with an underscore (_).
To make your extension available to other Dart libraries (which usually means other files), give it a name, e.g.:
extension DateTimeConversion on DateTime {
...
}