How to maintain backwards compabiliby when refactoring param name in published Flutter/Dart package?

Issue

I’m contributing to my first open source flutter library and had to change some variable names to make the project coherent.

The library owner pointed out the refactored params will break the apps for people updating from the previous version.
He asked me to use alias and set the old variables to deprecated, so that developers can change it to the next major realese.

I’ve done some googling but can’t find a tutorial of how to do such a thing in a professional manner.
I actually can’t find information on aliases at all.

Anyone out there who can help me to help the community?

Edit: before and after constructor

before:

CarouselSlider({
  @required
  this.items,
  this.viewportFraction: 0.8,
  this.initialPage: 0,
  this.aspectRatio: 16/9,
  this.height,
  this.realPage: 10000,
  this.autoPlay: false,
  this.interval: const Duration(seconds: 4),
  this.reverse: false,
  this.autoPlayCurve: Curves.fastOutSlowIn,
  this.autoPlayDuration: const Duration(milliseconds: 800),
  this.updateCallback,
  this.distortion: true,
})

After:

CarouselSlider({
  @required
  this.items,
  this.height,
  this.aspectRatio: 16/9,
  this.viewportFraction: 0.8,
  this.initialPage: 0,
  this.realPage: 10000,
  this.reverse: false,
  this.autoPlay: false,
  this.autoPlayInterval: const Duration(seconds: 4),
  this.autoPlayAnimationDuration: const Duration(milliseconds: 800),
  this.autoPlayCurve: Curves.fastOutSlowIn,
  this.enlargeCenterPage: false,
  this.pauseAutoPlayOnTouch,
  this.onPageChangedCallback,
})

Refactored names:

interval         --> autoPlayInterval
distortion       --> enlargeCenterPage
autoPlayDuration --> autoPlayAnimationDuration
updateCallback   --> onPageChanged

Solution

An example for the interval property

CarouselSlider({
  @required
  this.items,
  this.viewportFraction: 0.8,
  this.initialPage: 0,
  this.aspectRatio: 16/9,
  this.height,
  this.realPage: 10000,
  this.autoPlay: false,

  @Deprecated('use "autoplayInterval" instead') 
  Duration interval: const Duration(seconds: 4),
  Duration autoplayInterval,

  this.reverse: false,
  this.autoPlayCurve: Curves.fastOutSlowIn,
  this.autoPlayDuration: const Duration(milliseconds: 800),
  this.updateCallback,
  this.distortion: true,
}) : assert(interval == null || autoplayInterval == null, 'Use either "interval" or "autoPlayInterval", but not both.'), autoplayInterval = autoplayInterval ?? interval;

Answered By – Günter Zöchbauer

Answer Checked By – Willingham (FlutterFixes Volunteer)

Leave a Reply

Your email address will not be published. Required fields are marked *