# Flutter

## **Overview**

The Blitzllama Flutter SDK allows you to deliver in-app surveys seamlessly within your Flutter application. By integrating the SDK, you can collect targeted user feedback during critical moments in the user journey.

**Key Features**

* Quickly embed surveys via configurable triggers.
* Identify and track users to avoid duplicate surveys.
* Personalize surveys using user attributes and event properties (optional).
* Localize surveys by setting preferred languages (optional).

**Requirements**

* Access to the Blitzllama dashboard for API key retrieval and trigger configuration.

***

## Getting Started

### **Before You Begin**

1. **Obtain Your API Key:** Log in to the Blitzllama dashboard, navigate to the **Connections** tab, select **Flutter**, and retrieve your API key.
2. **Confirm Compatibility:** Ensure your Flutter project is set up and running.

### Quick Start (10 Minutes)

1. Open your `pubspec.yaml` file and add the following dependency:

```yaml
dependencies:
    blitzllama_flutter: ^0.6.3
```

2. Run the command:

```bash
flutter pub get
```

3. Import Blitzllama in your Dart code:

```dart
import 'package:blitzllama_flutter/blitzllama_flutter.dart';
```

4. Initialize the SDK: Add the initialization code in the file where Blitzllama functions will be invoked:

```dart
BlitzllamaFlutter.init("<api_key>");
```

If you face issues with iOS builds, add the following to your `Podfile`:

```ruby
target 'Runner' do
  use_frameworks!
  pod 'SwiftyJSON'
  flutter_install_all_ios_pods File.dirname(File.realpath(__FILE__))
end
```

{% hint style="info" %}
Check [**pub.dev**](https://pub.dev/packages/blitzllama_flutter) for usages and examples.
{% endhint %}

## Managing Users&#x20;

1. **Create a User:** Create a unique user ID to ensure consistency and avoid multiple surveys for the same user

```swift
BlitzllamaFlutter.createUser(user_id);
```

2. **Update User Email (Optional):** You can associate an email address with the user

```swift
BlitzllamaFlutter.setUserEmail(email)
```

3. **Update User Name (Optional):** Personalize surveys by associating a user name

```swift
BlitzllamaFlutter.setUserName(username)
```

4. **Set User Attributes (Optional):** Add user attributes for targeting specific cohorts

```swift
BlitzllamaFlutter.setUserAttribute(attribute, attributeValue, dataType); 
```

Allowed data\_type values are "string", "number", "date", and "boolean".

5. **Set Survey Language (Optional):** Define the preferred survey language in [ISO 639-1 codes](https://en.wikipedia.org/wiki/List_of_ISO_639-1_codes).

```swift
BlitzllamaFlutter.setSurveyLanguage("en");
```

6. **Logout User (Optional):** Clear the user session by calling

```swift
BlitzllamaFlutter.logout();
```

## Adding Triggers

1. **Triggering a Survey:** Add triggers to initiate surveys at key moments (e.g., button press or page load)

```swift
BlitzllamaFlutter.triggerEvent(triggerName);
```

2. **Adding Event Properties (Optional):** Attach properties to a trigger for advanced targeting. You can pass a map of key-value pairs as properties. For example:

```swift
BlitzllamaFlutter.triggerEvent(triggerName, eventProperties); 

//In below example, the event HomeActivity is triggered with the properties age and name. 
BlitzllamaFlutter.triggerEvent("HomeActivity", {"age": 10, "name": "John"});
```

These properties help segment and target users based on context, and they can be configured in the Blitzllama dashboard.

![](https://documentation.blitzllama.com/~gitbook/image?url=https%3A%2F%2F713176431-files.gitbook.io%2F%7E%2Ffiles%2Fv0%2Fb%2Fgitbook-x-prod.appspot.com%2Fo%2Fspaces%252F-MagfF5JPg-8vm99iTd9%252Fuploads%252FJFVinyFxj3YupHRvgZJB%252Fimage.png%3Falt%3Dmedia%26token%3D983225e2-fa1b-4476-8788-72016865f05c\&width=300\&dpr=4\&quality=100\&sign=d8aaa2de\&sv=1)

## Advanced Usage and Best Practices

### Custom Fonts

Customize your survey’s look and feel by specifying custom fonts:

```dart
BlitzllamaFlutter.setCustomFont("Roboto-Regular", "Roboto-Bold");
```

If the survey widget does not automatically apply the default font, use the method above to ensure the correct font is set.

## FAQ & Troubleshooting

1. **Survey not showing up?**
   1. Ensure `createUser` is called before `triggerEvent`.
   2. Verify that the event name matches the Blitzllama dashboard configuration.
2. **Survey not appearing on app launch?**
   1. Add a callback to the `createUser` function to handle potential race conditions:

```dart
BlitzllamaFlutter.createUser(user_id, onSuccess: () {
  BlitzllamaFlutter.triggerEvent("trigger_name");
});
```

3. **Switching users?**
   1. Call `logout()` before creating a new user.
4. **How to create triggers?**
   1. Add a new `trigger_name` in your app; it will automatically sync with the Blitzllama dashboard.
   2. Alternatively, configure the trigger in the Blitzllama dashboard first, then use it in your app.

## Next Steps

1. Start by adding triggers for the top 5 critical user actions in your app.
2. Create your first survey on the Blitzllama dashboard.
3. Customize the survey UI/UX on the dashboard to align with your brand.

For additional support, reach out via support chat or email at <tech@blitzllama.com>.
