Skip to content

Commit

Permalink
Replace init() with isBuilt
Browse files Browse the repository at this point in the history
  • Loading branch information
Mercy811 committed Mar 12, 2024
1 parent f23e3bb commit 53713ff
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
2 changes: 1 addition & 1 deletion example/lib/my_app.dart
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ class _MyAppState extends State<MyApp> {
late Amplitude analytics;

initAnalytics() async {
await analytics.init();
await analytics.isBuilt;

setMessage('Amplitude initialized');
}
Expand Down
30 changes: 20 additions & 10 deletions lib/amplitude.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,36 @@ import 'package:amplitude_flutter/events/group_identify_event.dart';
class Amplitude {
Configuration configuration;
MethodChannel _channel = const MethodChannel("amplitude_flutter");

/// Returns an Amplitude instance
/// Whether the Amplitude instance has been successfully initialized
///
/// Call `init()` to initialize underlying SDKs on native platforms
/// ```
/// var amplitude = Amplitude(Configuration(apiKey: "apiKey"));
/// await amplitude.init();
/// // If care about init complete
/// await amplitude.isBuilt;
/// ```
Amplitude(this.configuration);
late Future<bool> isBuilt;

/// Initializes an Amplitude instance
/// Returns an Amplitude instance
///
/// ```
/// var amplitude = Amplitude(Configuration(apiKey: "apiKey"));
/// await amplitude.init();
/// // If care about init complete
/// await amplitude.isBuilt;
/// ```
Future<void> init([MethodChannel? methodChannel]) async {
Amplitude(this.configuration, [MethodChannel? methodChannel]){
_channel = methodChannel ?? this._channel;
return await _channel.invokeMethod(
"init", this.configuration.toMap());
isBuilt = _init();
}

/// Private method to initialize and return a Future<bool>
Future<bool> _init() async {
try {
await _channel.invokeMethod("init", configuration.toMap());
return true; // Initialization successful
} catch (e) {
print("Error initializing Amplitude: $e");
return false; // Initialization failed
}
}

/// Tracks an event. Events are saved locally.
Expand Down
4 changes: 2 additions & 2 deletions test/amplitude_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,8 @@ void main() {
mockChannel = MockMethodChannel();
when(mockChannel.invokeListMethod("init", any))
.thenAnswer((_) async => null);
amplitude = Amplitude(testConfiguration);
await amplitude.init(mockChannel);
amplitude = Amplitude(testConfiguration, mockChannel);
await amplitude.isBuilt;
});

test("Should init and track call MethodChannel", () async {
Expand Down

0 comments on commit 53713ff

Please sign in to comment.