Important Note:
This article has been moved to our Developer Center, and will no longer be updated on this site. Here is the newest version of our Android Flutter SDK Guide.
Flutter is an open-source UI Software Development Kit created by Google. It is used to develop applications for Android, iOS, Windows, Mac, Linux, Google Fuchsia and the Web.
This article will help you get started by walking you though the the installation process, explaining how to add Carrier Information and provide you with an example on how to do it.
Table of Contents
Getting Started
To use this plugin, add amplitude_flutter as a dependency in your pubspec.yaml file. Please note that for this process the Amplitude API Key is necessary. This is found in Amplitude, in Settings > Projects. Select the necessary project, and under Project Settings you will be able to find the API key.
Once this is done, you will need to import the package: amplitude_flutter/amplitude_flutter.dart and instantiate AmplitudeFlutter wit your API Key.
In addition, a Config object can be passed as a constructor argument for additional options.
Important Note:
- This plugin's methods should only be called from the main isolate.
- To find our most up to date version of the SDK, please feel free to check out our Release Log on Github here.
- The Flutter SDK version 1.2 .0 can track the Advertising ID. Previous versions lack that capability.
Installation
To successfully install and use the Flutter SDK, please make sure to follow the bellow steps:
- Import the Amplitude Flutter SDK, then go to the pubspec.yaml file and add our dependency to it. Dependencies:
amplitude_flutter: ^1.1.1
- Install it and then run ''flutter pub get'' in the terminal to install the SDK;
- Import our SDK in my_app.dart at the very beginning:
import ‘package:amplitude_flutter/amplitude_flutter.dart';
- Declare a property inside my_app.dart, something like: "AmplitudeFlutter Analytics";
- Once this is done, locate the my_app.dart file and add the following code to the lifecycle function 'initState' of _MyAppState:
analytics = AmplitudeFlutter('API_KEY', Config(bufferSize: 8, getCarrierInfo: true));
- If and when you need to call any API's in our SDKs, you can simply:
final Revenue revenue = Revenue()
..setProductId(productId.text)
..setPrice(num.tryParse(price.text))
..setQuantity(num.tryParse(quantity.text));
AppState.of(context)
..analytics.logRevenue(revenue)
..setMessage('Revenue Sent.');
Important Note:
- We provide an example Flutter App for you to easily learn how to use our SDK. Please feel free to check out this link for more details.
Adding Carrier Information
You can set an option in the config object titled getCarrierInfo to retrieve carrier name for a device. This is the ( Config). This object can be passed as a constructor argument for additional options.
If you set getCarrierInfo to true - recipients on Android devices will see a dialog box asking them for permission. This dialog will say allow app to make and manage phone calls. This is a message sent from the android operating system for the READ_PHONE_STATE permission and carrier info is grouped into this. If the user denies permission - carrier information will not be retrieved. The new android operating systems require asking a user for permission before retrieving this information.
By default the Config will set getCarrierInfo to false.
Example Request
import 'package:amplitude_flutter/amplitude_flutter.dart'; // replace 'API_KEY' with your project's API_KEY Future<void> example() async { final AmplitudeFlutter analytics = AmplitudeFlutter('API KEY'); // set this user's id analytics.setUserId('abc123'); // log an event analytics.logEvent(name: 'add_friend'); // Log events with event properties analytics.logEvent(name: 'add_friend', properties: { 'event_properties': { 'key': 'value' }}); // identify a user final Identify identify = Identify() ..set('cohort', 'Test A') ..setOnce('completed_onboarding', 'true') ..add('login_count', 1) ..append('tags', 'new tag') ..unset('demo_user'); analytics.identify(identify); // Amplitude Accounts [https://amplitude.zendesk.com/hc/en-us/articles/115001765532-Accounts] methods: // add a user to a group analytics.setGroup('orgId', 15); // change properties of a group analytics.groupIdentify('orgId', 15, Identify()..set('account_manager', 456)); // emit an event associated with a group analytics.logEvent('Demo Released', properties: { 'groups': { 'orgId': 15 } }); // Log revenue final Revenue revenue = Revenue() ..setPrice(23.23) ..setQuantity(3) ..setProductId('widget1') analytics.logRevenue(revenue); }