From 18c092ca52fbbad14a9c9adf8ddaee643dad0932 Mon Sep 17 00:00:00 2001 From: Jonny Borges <35742643+jonataslaw@users.noreply.github.com> Date: Sat, 10 Apr 2021 03:45:50 -0300 Subject: [PATCH 1/3] migrate to flutter 2 --- example/lib/generated_plugin_registrant.dart | 15 +++++++++ example/lib/main.dart | 35 +++++++++++--------- lib/src/posthog.dart | 28 ++++++++-------- lib/src/posthog_default_options.dart | 2 +- lib/src/posthog_method_channel.dart | 25 +++++++------- lib/src/posthog_observer.dart | 12 +++---- lib/src/posthog_platform_interface.dart | 25 +++++++------- lib/src/posthog_web.dart | 2 -- pubspec.yaml | 2 +- 9 files changed, 79 insertions(+), 67 deletions(-) create mode 100644 example/lib/generated_plugin_registrant.dart diff --git a/example/lib/generated_plugin_registrant.dart b/example/lib/generated_plugin_registrant.dart new file mode 100644 index 0000000..c98a025 --- /dev/null +++ b/example/lib/generated_plugin_registrant.dart @@ -0,0 +1,15 @@ +// +// Generated file. Do not edit. +// + +// ignore_for_file: lines_longer_than_80_chars + +import 'package:posthog_flutter/src/posthog_web.dart'; + +import 'package:flutter_web_plugins/flutter_web_plugins.dart'; + +// ignore: public_member_api_docs +void registerPlugins(Registrar registrar) { + PosthogWeb.registerWith(registrar); + registrar.registerMessageHandler(); +} diff --git a/example/lib/main.dart b/example/lib/main.dart index d48b20b..9d64da1 100644 --- a/example/lib/main.dart +++ b/example/lib/main.dart @@ -21,7 +21,7 @@ void main() { /// /// Aside from this special use case, any other context property that needs /// to be defined (or re-defined) can be done. - Posthog.setContext({ + Posthog().setContext({ 'device': { 'token': 'testing', } @@ -33,7 +33,7 @@ void main() { class MyApp extends StatelessWidget { @override Widget build(BuildContext context) { - Posthog.screen( + Posthog().screen( screenName: 'Example Screen', ); return MaterialApp( @@ -45,10 +45,10 @@ class MyApp extends StatelessWidget { children: [ Spacer(), Center( - child: FlatButton( + child: TextButton( child: Text('CAPTURE ACTION WITH POSTHOG'), onPressed: () { - Posthog.capture( + Posthog().capture( eventName: 'ButtonClicked', properties: { 'foo': 'bar', @@ -61,49 +61,52 @@ class MyApp extends StatelessWidget { ), Spacer(), Center( - child: FlatButton( + child: TextButton( child: Text('Update Context'), onPressed: () { - Posthog.setContext({'custom': 123}); + Posthog().setContext({'custom': 123}); }, ), ), Spacer(), Center( - child: FlatButton( + child: TextButton( child: Text('Clear Context'), onPressed: () { - Posthog.setContext({}); + Posthog().setContext({}); }, ), ), Spacer(), Center( - child: FlatButton( + child: TextButton( child: Text('Disable'), onPressed: () async { - await Posthog.disable(); - Posthog.capture(eventName: 'This event will not be logged'); + await Posthog().disable(); + Posthog().capture( + eventName: 'This event will not be logged', + properties: {}); }, ), ), Spacer(), Center( - child: FlatButton( + child: TextButton( child: Text('Enable'), onPressed: () async { - await Posthog.enable(); - Posthog.capture(eventName: 'Enabled capturing events!'); + await Posthog().enable(); + Posthog().capture( + eventName: 'Enabled capturing events!', properties: {}); }, ), ), Spacer(), Platform.isIOS ? Center( - child: FlatButton( + child: TextButton( child: Text('Debug mode'), onPressed: () { - Posthog.debug(true); + Posthog().debug(true); }, ), ) diff --git a/lib/src/posthog.dart b/lib/src/posthog.dart index 893c9b3..1b67956 100644 --- a/lib/src/posthog.dart +++ b/lib/src/posthog.dart @@ -1,6 +1,4 @@ import 'dart:io'; - -import 'package:meta/meta.dart'; import 'package:posthog_flutter/src/posthog_platform_interface.dart'; export 'package:posthog_flutter/src/posthog_observer.dart'; @@ -15,12 +13,12 @@ class Posthog { return _instance; } - String currentScreen; + String? currentScreen; Future identify({ - @required userId, - Map properties, - Map options, + required userId, + Map? properties, + Map? options, }) { return _posthog.identify( userId: userId, @@ -30,9 +28,9 @@ class Posthog { } Future capture({ - @required String eventName, - Map properties, - Map options, + required String eventName, + required Map properties, + Map? options, }) { if (!properties.containsKey('\$screen_name')) { properties['\$screen_name'] = this.currentScreen; @@ -45,9 +43,9 @@ class Posthog { } Future screen({ - @required String screenName, - Map properties, - Map options, + required String screenName, + Map? properties, + Map? options, }) { if (screenName != '/') { this.currentScreen = screenName; @@ -60,8 +58,8 @@ class Posthog { } Future alias({ - @required String alias, - Map options, + required String alias, + Map? options, }) { return _posthog.alias( alias: alias, @@ -69,7 +67,7 @@ class Posthog { ); } - Future get getAnonymousId { + Future get getAnonymousId { return _posthog.getAnonymousId; } diff --git a/lib/src/posthog_default_options.dart b/lib/src/posthog_default_options.dart index 957aa87..2d84013 100644 --- a/lib/src/posthog_default_options.dart +++ b/lib/src/posthog_default_options.dart @@ -1,5 +1,5 @@ class PosthogDefaultOptions { - Map options; + Map? options; /// Singleton of [PosthogDefaultOptions]. static final PosthogDefaultOptions instance = PosthogDefaultOptions._(); diff --git a/lib/src/posthog_method_channel.dart b/lib/src/posthog_method_channel.dart index 625353b..38853d4 100644 --- a/lib/src/posthog_method_channel.dart +++ b/lib/src/posthog_method_channel.dart @@ -1,4 +1,3 @@ -import 'package:meta/meta.dart'; import 'package:flutter/services.dart'; import 'package:posthog_flutter/src/posthog_default_options.dart'; import 'package:posthog_flutter/src/posthog_platform_interface.dart'; @@ -7,9 +6,9 @@ const MethodChannel _channel = MethodChannel('posthogflutter'); class PosthogMethodChannel extends PosthogPlatform { Future identify({ - @required userId, - Map properties, - Map options, + required userId, + Map? properties, + Map? options, }) async { try { await _channel.invokeMethod('identify', { @@ -23,9 +22,9 @@ class PosthogMethodChannel extends PosthogPlatform { } Future capture({ - @required String eventName, - Map properties, - Map options, + required String eventName, + Map? properties, + Map? options, }) async { try { await _channel.invokeMethod('capture', { @@ -39,9 +38,9 @@ class PosthogMethodChannel extends PosthogPlatform { } Future screen({ - @required String screenName, - Map properties, - Map options, + required String screenName, + Map? properties, + Map? options, }) async { try { await _channel.invokeMethod('screen', { @@ -55,8 +54,8 @@ class PosthogMethodChannel extends PosthogPlatform { } Future alias({ - @required String alias, - Map options, + required String alias, + Map? options, }) async { try { await _channel.invokeMethod('alias', { @@ -68,7 +67,7 @@ class PosthogMethodChannel extends PosthogPlatform { } } - Future get getAnonymousId async { + Future get getAnonymousId async { return await _channel.invokeMethod('getAnonymousId'); } diff --git a/lib/src/posthog_observer.dart b/lib/src/posthog_observer.dart index 5f006c3..1945105 100644 --- a/lib/src/posthog_observer.dart +++ b/lib/src/posthog_observer.dart @@ -1,9 +1,9 @@ import 'package:flutter/widgets.dart'; import 'package:posthog_flutter/posthog_flutter.dart'; -typedef String ScreenNameExtractor(RouteSettings settings); +typedef String? ScreenNameExtractor(RouteSettings settings); -String defaultNameExtractor(RouteSettings settings) => settings.name; +String? defaultNameExtractor(RouteSettings settings) => settings.name; class PosthogObserver extends RouteObserver> { PosthogObserver({this.nameExtractor = defaultNameExtractor}); @@ -11,14 +11,14 @@ class PosthogObserver extends RouteObserver> { final ScreenNameExtractor nameExtractor; void _sendScreenView(PageRoute route) { - final String screenName = nameExtractor(route.settings); + final String? screenName = nameExtractor(route.settings); if (screenName != null) { Posthog().screen(screenName: screenName); } } @override - void didPush(Route route, Route previousRoute) { + void didPush(Route route, Route? previousRoute) { super.didPush(route, previousRoute); if (route is PageRoute) { _sendScreenView(route); @@ -26,7 +26,7 @@ class PosthogObserver extends RouteObserver> { } @override - void didReplace({Route newRoute, Route oldRoute}) { + void didReplace({Route? newRoute, Route? oldRoute}) { super.didReplace(newRoute: newRoute, oldRoute: oldRoute); if (newRoute is PageRoute) { _sendScreenView(newRoute); @@ -34,7 +34,7 @@ class PosthogObserver extends RouteObserver> { } @override - void didPop(Route route, Route previousRoute) { + void didPop(Route route, Route? previousRoute) { super.didPop(route, previousRoute); if (previousRoute is PageRoute && route is PageRoute) { _sendScreenView(previousRoute); diff --git a/lib/src/posthog_platform_interface.dart b/lib/src/posthog_platform_interface.dart index d1fdd40..69bec1a 100644 --- a/lib/src/posthog_platform_interface.dart +++ b/lib/src/posthog_platform_interface.dart @@ -1,4 +1,3 @@ -import 'package:meta/meta.dart'; import 'package:posthog_flutter/src/posthog_method_channel.dart'; abstract class PosthogPlatform { @@ -12,37 +11,37 @@ abstract class PosthogPlatform { static PosthogPlatform instance = PosthogMethodChannel(); Future identify({ - @required userId, - Map properties, - Map options, + required userId, + Map? properties, + Map? options, }) { throw UnimplementedError('identify() has not been implemented.'); } Future capture({ - @required String eventName, - Map properties, - Map options, + required String eventName, + Map? properties, + Map? options, }) { throw UnimplementedError('capture() has not been implemented.'); } Future screen({ - @required String screenName, - Map properties, - Map options, + required String screenName, + Map? properties, + Map? options, }) { throw UnimplementedError('screen() has not been implemented.'); } Future alias({ - @required String alias, - Map options, + required String alias, + Map? options, }) { throw UnimplementedError('alias() has not been implemented.'); } - Future get getAnonymousId { + Future get getAnonymousId { throw UnimplementedError('getAnonymousId() has not been implemented.'); } diff --git a/lib/src/posthog_web.dart b/lib/src/posthog_web.dart index 5dd25c3..effe7d6 100644 --- a/lib/src/posthog_web.dart +++ b/lib/src/posthog_web.dart @@ -8,7 +8,6 @@ class PosthogWeb { final MethodChannel channel = MethodChannel( 'posthog_flutter', const StandardMethodCodec(), - registrar.messenger, ); final PosthogWeb instance = PosthogWeb(); channel.setMethodCallHandler(instance.handleMethodCall); @@ -44,7 +43,6 @@ class PosthogWeb { final user = analytics.callMethod('user'); final anonymousId = user.callMethod('anonymousId'); return anonymousId; - break; case 'reset': analytics.callMethod('reset'); break; diff --git a/pubspec.yaml b/pubspec.yaml index a198c44..8e0f635 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -7,7 +7,7 @@ issue_tracker: https://github.com/posthog/posthog-flutter/issues documentation: https://github.com/posthog/posthog-flutter#readme environment: - sdk: ">=2.1.0 <3.0.0" + sdk: '>=2.12.0 <3.0.0' flutter: ">=1.12.13+hotfix.4 <2.0.0" dependencies: From 91ecfefd2e573a834a10be2dda631f2c8ef713fc Mon Sep 17 00:00:00 2001 From: Jonny Borges <35742643+jonataslaw@users.noreply.github.com> Date: Sat, 10 Apr 2021 03:53:59 -0300 Subject: [PATCH 2/3] bump version --- CHANGELOG.md | 4 ++++ pubspec.yaml | 5 ++--- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 621aeae..718c2c9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +## 2.0.0 + +- Migrate to flutter 2 + ## 1.11.2 - Bump and pin version for Android lib to 1.1.1 because of bug diff --git a/pubspec.yaml b/pubspec.yaml index 8e0f635..6762b98 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -1,6 +1,6 @@ name: posthog_flutter description: Flutter implementation of Posthog client for iOS, Android and Web -version: 1.11.2 +version: 2.0.0 homepage: https://www.posthog.com repository: https://github.com/posthog/posthog-flutter issue_tracker: https://github.com/posthog/posthog-flutter/issues @@ -8,14 +8,13 @@ documentation: https://github.com/posthog/posthog-flutter#readme environment: sdk: '>=2.12.0 <3.0.0' - flutter: ">=1.12.13+hotfix.4 <2.0.0" dependencies: flutter: sdk: flutter flutter_web_plugins: sdk: flutter - meta: ^1.0.0 + dev_dependencies: flutter_test: From f5c613715b6425445b58ca2d1d7781c6eac5a027 Mon Sep 17 00:00:00 2001 From: Jonny Borges <35742643+jonataslaw@users.noreply.github.com> Date: Fri, 4 Jun 2021 14:15:28 -0300 Subject: [PATCH 3/3] made properties nullable rather required --- lib/src/posthog.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/posthog.dart b/lib/src/posthog.dart index 1b67956..b266070 100644 --- a/lib/src/posthog.dart +++ b/lib/src/posthog.dart @@ -29,10 +29,10 @@ class Posthog { Future capture({ required String eventName, - required Map properties, + Map? properties, Map? options, }) { - if (!properties.containsKey('\$screen_name')) { + if (properties != null && !properties.containsKey('\$screen_name')) { properties['\$screen_name'] = this.currentScreen; } return _posthog.capture(