What is deprecated how to use it with methods in flutter with sample code ?
#flutter
Answer
@deprecated Annotation in Flutter
@deprecated marks APIs as no longer recommended. It warns developers to use alternatives.
Basic Usage
dartimport 'package:meta/meta.dart'; ('Use newFunction() instead') void oldFunction() { print('This is deprecated'); } void newFunction() { print('Use this instead'); } void main() { oldFunction(); // Warning: "deprecated" newFunction(); // OK }
Deprecating Methods
dartclass User { String name = 'Alice'; ('Use printName() instead') void displayName() { print(name); } void printName() { print(name); } }
Deprecating Classes
dart('Use NewUserWidget instead') class OldUserWidget extends StatelessWidget { Widget build(BuildContext context) { return Text('Old'); } } class NewUserWidget extends StatelessWidget { Widget build(BuildContext context) { return Text('New'); } }
Deprecating Constructors
dartclass Config { String apiUrl; ('Use Config.fromEnv() instead') Config(this.apiUrl); Config.fromEnv() : apiUrl = String.fromEnvironment('API_URL'); }
Best Practice: Provide migration path in message.