Skip to content

Commit

Permalink
fix(rudder_sdk_flutter_example): fixed the issue of automatic screen …
Browse files Browse the repository at this point in the history
…events being sent before SDK initialization
  • Loading branch information
Desu Sai Venkat committed Apr 5, 2023
1 parent 0f0d134 commit 9f43689
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 2 deletions.
3 changes: 3 additions & 0 deletions packages/example/lib/home_screen.dart
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import 'package:rudder_integration_firebase_flutter/rudder_integration_firebase_
// ignore: depend_on_referenced_packages
import 'package:rudder_sdk_flutter_platform_interface/platform.dart';

bool isInitialized = false;

class HomeScreen extends StatefulWidget {
const HomeScreen({Key? key}) : super(key: key);

Expand Down Expand Up @@ -42,6 +44,7 @@ class HomeScreenState extends State<HomeScreen> {
builder.withLogLevel(RudderLogger.VERBOSE);
String writeKey = dotenv.env['WRITE_KEY'] ?? "INVALID_WRITE_KEY";
rudderClient.initialize(writeKey, config: builder.build(), options: null);
isInitialized = true;

setOutput("initialize:\nwriteKey: $writeKey");
}
Expand Down
9 changes: 7 additions & 2 deletions packages/example/lib/my_route_observer.dart
Original file line number Diff line number Diff line change
@@ -1,20 +1,25 @@
import 'package:flutter/material.dart';
import 'package:rudder_sdk_flutter/RudderController.dart';
import 'home_screen.dart';

class MyRouteObserver extends RouteObserver<PageRoute<dynamic>> {
@override
void didPush(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPush(route, previousRoute);
if (route is PageRoute && route.settings.name != null) {
RudderController.instance.screen(route.settings.name!);
if (isInitialized) {
RudderController.instance.screen(route.settings.name!);
}
}
}

@override
void didPop(Route<dynamic> route, Route<dynamic>? previousRoute) {
super.didPop(route, previousRoute);
if (previousRoute is PageRoute && route is PageRoute) {
RudderController.instance.screen(previousRoute.settings.name!);
if (isInitialized) {
RudderController.instance.screen(previousRoute.settings.name!);
}
}
}
}

0 comments on commit 9f43689

Please sign in to comment.