lschmierer / android_play_install_referrer

A Flutter plugin for the Android Play Install Referrer API.
BSD 2-Clause "Simplified" License
21 stars 21 forks source link

Migrate to null safety #10

Closed san-smith closed 3 years ago

san-smith commented 3 years ago

Hello! I have added null safety support for the package, this will help migrate projects that use your package to newer flutter versions.

lschmierer commented 3 years ago

Looks good, going to push a new version do pubdev right away.

lschmierer commented 3 years ago

Just publisher version 0.1

san-smith commented 3 years ago

Thanks!

lschmierer commented 3 years ago

Unfortunately this introduced some issues (see https://github.com/lschmierer/android_play_install_referrer/issues/11).

I think changing


  late final String _installReferrer;
  late final int _referrerClickTimestampSeconds;
  late final int _installBeginTimestampSeconds;
  late final int _referrerClickTimestampServerSeconds;
  late final int _installBeginTimestampServerSeconds;
  late final String _installVersion;
  late final bool _googlePlayInstantParam;

to


  late final String? _installReferrer;
  late final int? _referrerClickTimestampSeconds;
  late final int? _installBeginTimestampSeconds;
  late final int? _referrerClickTimestampServerSeconds;
  late final int? _installBeginTimestampServerSeconds;
  late final String? _installVersion;
  late final bool ?_googlePlayInstantParam;

will fix this. By the way, why was it necessary to the late keyword?

san-smith commented 3 years ago

If you are using nullable types (like String?), then the late keyword is not necessary.

But we have to understand, can details really have null values for these keys?

val details = HashMap<String, Any?>()
details["installReferrer"] = referrerDetails.installReferrer
details["referrerClickTimestampSeconds"] = referrerDetails.referrerClickTimestampSeconds
details["installBeginTimestampSeconds"] = referrerDetails.installBeginTimestampSeconds
details["referrerClickTimestampServerSeconds"] = referrerDetails.referrerClickTimestampServerSeconds
details["installBeginTimestampServerSeconds"] = referrerDetails.installBeginTimestampServerSeconds
details["installVersion"] = referrerDetails.installVersion
details["googlePlayInstantParam"] = referrerDetails.googlePlayInstantParam

If so, it is better to use types with ?.

san-smith commented 3 years ago

By the way, why was it necessary to the late keyword?

In short: it can be necessary if we use non nullable type variable without initialization. More you can read about the late keyword in the documentation.

lschmierer commented 3 years ago

I have changed the two Strings to String?. ReferrerDetails reads its values from a BaseBundle.

Screen Shot 2021-03-16 at 17 46 28

BaseBundle.getString can return null. getLong and getBoolean return default values, so they can not be null.

https://developer.android.com/reference/android/os/BaseBundle

In short: it can be necessary if we use non nullable type variable without initialization.

The non-null variables are initialized in the constructor. I don't see where late initialization is necessary.