# Monitoring Flutter Apps End To End With Dynatrace

**URL:** <https://community.dynatrace.com/t/monitoring-flutter-apps-end-to-end-with-dynatrace/50027>\
**Category:** Dynatrace Tips\
**Tags:** flutter, technologies, mobile-monitoring, tips-and-tricks\
**Created:** [January 31, 2025, 11:19am UTC](https://community.dynatrace.com/t/monitoring-flutter-apps-end-to-end-with-dynatrace/50027 "2025-01-31T11:19:03Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![zaid-bashir](https://dub1.discourse-cdn.com/dynatrace/user_avatar/community.dynatrace.com/zaid-bashir/32/22651_2.png) [@zaid-bashir](https://community.dynatrace.com/u/zaid-bashir)\
**Post date:** [January 31, 2025, 11:19am UTC](https://community.dynatrace.com/t/monitoring-flutter-apps-end-to-end-with-dynatrace/50027/1 "2025-01-31T11:19:03Z")

</div>

Monitoring Flutter apps with Dynatrace using the default/automatic setup provides basic features such as user actions (For Startup Mobile App Page Only), user session (Anonymous) and connected services ( **Always Showing Zero Connected Services** ). For comprehensive end-to-end monitoring—including user sessions with API calls, user tagging, waterfall analysis, web requests, service flow, reporting values or errors, user actions, and API tracking, follow the below guide.

### **Step 1: Setting Up Mobile App Monitoring in Dynatrace**

**1) Create a Mobile App in Dynatrace**:

- Navigate to the “Mobile App” section in Dynatrace using the search menu.
- Click on “Create Mobile App.”
- Name your mobile app.

 ![zaidbashir_1-1738311565946.png](https://global.discourse-cdn.com/dynatrace/original/2X/1/12068c57e3007973a5df29bc381e3df94b677c20.png)

 ![zaidbashir_2-1738311648826.png](https://global.discourse-cdn.com/dynatrace/original/3X/4/5/45ba514fd231d5eb13da797e53811db5b8e0be29.png)

 ![zaidbashir_3-1738311749991.png](https://global.discourse-cdn.com/dynatrace/original/3X/3/9/39db18b2de216002aacd1d8352afbae3124d8236.png)

**2) Select Flutter as the Technology**:

- In the Instrumentation Wizard, choose&nbsp; **Flutter** &nbsp;as the technology.
- Follow the instructions provided, such as adding packages, updating the&nbsp;pubspec.yaml&nbsp;file, and inserting a code snippet to initialize the Flutter plugin when the app starts.

 ![zaidbashir_4-1738311840789.png](https://global.discourse-cdn.com/dynatrace/original/2X/2/2244abc11530ba3601e97b69759f50824b04efc2.png)

**Important Notes** :

- The package version mentioned in Dynatrace might be outdated. Visit the&nbsp;[official Dynatrace Flutter plugin page](https://pub.dev/packages/dynatrace_flutter_plugin)&nbsp;to get the latest version (currently&nbsp;dynatrace\_flutter\_plugin 3.305.2).
- Add the package to your&nbsp;pubspec.yaml&nbsp;as shown below&nbsp;file and run&nbsp;flutter pub get&nbsp;to fetch it.

 ![zaidbashir_5-1738312484926.png](https://global.discourse-cdn.com/dynatrace/original/2X/5/511d4269a9571c02134a97d664349b31cd113b01.png)  
**3)&nbsp;Download and Configure&nbsp;dynatrace.config.yaml**:

- Download the&nbsp;dynatrace.config.yaml&nbsp;file from Dynatrace and place it in the root folder of your Flutter project.
- Modify the&nbsp;main()&nbsp;function in your Flutter app to initialize the Dynatrace plugin.

 ![zaidbashir_6-1738312714470.png](https://global.discourse-cdn.com/dynatrace/original/3X/0/3/03d46f32e5cff8df75574f0f3fd449f28e77cb1e.png)

Below is the image that shows the code to modify the main() function.

 ![zaidbashir_7-1738312874383.png](https://global.discourse-cdn.com/dynatrace/original/3X/3/0/304f247096a2aef0938be463d517e43a09f0c464.png)

**4) Apply Configuration**:

- Run the command&nbsp;dart run dynatrace\_flutter\_plugin&nbsp;to apply the configuration.
- Once completed, you should start seeing data in your Dynatrace tenant.

 ![zaidbashir_8-1738312948997.png](https://global.discourse-cdn.com/dynatrace/original/3X/9/8/984f062e8b73745e5c4e626400fd23049c8f6aa9.png)

 ![zaidbashir_9-1738317117221.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/92d63b8f5462bc83de3c5df21b92288ebebbd61e.png)

### **Step 2: Advanced Configurations**

**1) Track User Actions and Web Requests**:

- Refer to the&nbsp;[official Dynatrace Flutter plugin documentation](https://pub.dev/packages/dynatrace_flutter_plugin)&nbsp;for advanced configurations like tracking user actions, web requests, reporting values, and handling errors.
- Avoid modifying the&nbsp;dynatrace.config.yaml&nbsp;file; make changes directly in your code.

 ![Screenshot 2025-01-31 121317.png](https://global.discourse-cdn.com/dynatrace/original/2X/c/cf0365e8cd73a8f8647958ed71cd5f3145200d0e.png)

**2) Update the&nbsp;main()&nbsp;Function**:

- Modify the&nbsp;main()&nbsp;function to include additional configurations like crash reporting, log levels, and certificate validation.

```java
void main() => Dynatrace().start(const MyApp(),
    configuration: const Configuration(
      reportCrash: true,
      logLevel: LogLevel.Info,
      certificateValidation: false,
      userOptIn: false,
    ));

```

**3) Track Navigation Changes**:

- Add a&nbsp;DynatraceNavigationObserver&nbsp;to the&nbsp;MaterialApp&nbsp;widget to track page navigation changes&nbsp;dart.

```java
Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Flutter Mobile',
      home: const SomeWidget(),
      navigatorObservers: [
        DynatraceNavigationObserver(),
      ],
    );
  }

```

### **Step 3: Monitoring REST APIs**

1. **Add the Dio Plugin** :
  - Use the&nbsp;dynatrace\_flutter\_plugin\_dio&nbsp;package (version&nbsp;3.305.2) to monitor REST API calls.
  - Add the package to your&nbsp;pubspec.yaml&nbsp;file and run&nbsp;flutter pub get.

2. **Create a Dynatrace HTTP Client** :
  - Create a Dynatrace HTTP client and an action to track API calls.

```java
var client = Dynatrace().createHttpClient();

```

```java
DynatraceRootAction? webAction = Dynatrace().enterAction(endPoint);

```

**Make API Calls** :

- Use the Dynatrace client to make API calls and close the action and client after the request is complete, according to your design patterns.

```java
final response = await client.get(Uri.parse(url));

```

```java
webAction?.leaveAction();
client.close();

```

**Complete Example Of the API Call is shown below.**

```java
Future<Map<String, dynamic>> fetchProducts() async {
    const String url = 'http://52.90.50.12:8080/api/products';
    try {
      final response = await client.get(Uri.parse(url));
      print(response.headers);

      if (response.statusCode == 200) {
        final Map<String, dynamic> data = jsonDecode(response.body);
        return data;
      } else {
        throw Exception('Failed to load products');
      }
    } catch (e) {
      throw Exception('Failed to load products');
    } finally {
      webAction?.leaveAction();
      client.close();
    }
  }

```

Once You finish this all custom configurations, restart the App and hit the API Calls from the Flutter Mobile App and after 2 or 3 minutes, you will see **Web Requests** & **Connected Services** &nbsp;.

 ![zaidbashir_10-1738318874566.png](https://global.discourse-cdn.com/dynatrace/original/2X/9/92dfc160d731487712aa4cabd7b620a19c94233f.png)

Also You can see the service flow as well as shown below.

 ![zaidbashir_11-1738319009548.png](https://global.discourse-cdn.com/dynatrace/original/3X/1/4/14dff75c76587055c9b47b0b310dff59a82bfc69.png)

### **Step 4: Tracking Parent and Child Widgets**

1. **Use&nbsp;DynatraceRootAction&nbsp;and&nbsp;DynatraceAction** :
  - Track interactions with parent and child widgets using&nbsp;DynatraceRootAction&nbsp;and&nbsp;DynatraceAction.

```java
DynatraceRootAction myAction = Dynatrace().enterAction("MyButton tapped");
DynatraceAction mySubAction = myAction.enterAction("MyButton Sub Action");

mySubAction.leaveAction();
myAction.leaveAction();

```

**Example Implementation** :

- Here’s an example of tracking actions in a&nbsp;CheckoutPage&nbsp;widget:

```java
class CheckoutPage extends StatefulWidget {
  const CheckoutPage({super.key});

  @override
  State<CheckoutPage> createState() => _CheckoutPageState();
}

class _CheckoutPageState extends State<CheckoutPage> {
  DynatraceRootAction? parentAction;
  @override
  void initState() {
    super.initState();
    parentAction =
        Dynatrace().enterAction("Checkout Page Initialised");
  }

  @override
  void dispose() {
    super.dispose();
    parentAction!.leaveAction();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Checkout"),
        centerTitle: true,
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Container(
              width: MediaQuery.of(context).size.width * 0.7,
              height: 60,
              margin: const EdgeInsets.symmetric(vertical: 10),
              child: ElevatedButton(
                style: ElevatedButton.styleFrom(
                  backgroundColor: Colors.black,
                  foregroundColor: Colors.white,
                  shape: RoundedRectangleBorder(
                    borderRadius: BorderRadius.circular(8), // Rounded corners
                  ),
                ),
                onPressed: () {
                  DynatraceAction childAction = parentAction!.enterAction("Pay Online Button Clicked");
                  Navigator.push(
                      context,
                      MaterialPageRoute(
                          builder: (context) => const ProductListScreen()));
                  childAction.leaveAction();
                },
                child: const Text(
                  "Pay Online",
                  style: TextStyle(
                    fontSize: 16,
                  ),
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}

```

### **Step 5: Reporting Values and Errors**

1. **Report Values** :
  - Use methods like&nbsp;reportStringValue,&nbsp;reportIntValue, and&nbsp;reportDoubleValue&nbsp;to report values during user interactions to dynatrace.

```java
myAction.reportStringValue("totalPrice", "${price}");

```

There are also other values that we can report while using App as shown below to Dynatrace.

```java
void reportError(String errorName, int errorCode, {Platform platform})
void reportErrorStacktrace(
      String errorName, String errorValue, String reason, String stacktrace,
      {Platform platform});
void reportEvent(String eventName, {Platform platform})
void reportIntValue(String valueName, int value, {Platform platform})
void reportDoubleValue(String valueName, double value, {Platform platform})

```

### **Step 6: User Tagging & Exiting Sessions**

1. **User Tagging** :

User tagging allows you to track and identify individual users or user sessions by assigning custom tags&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; (username,email,user\_id), which will help you to recognize which session is related to which user.

Below is the code snippet given which you will put inside the login functionality or AutoLogin Functionality&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; based&nbsp; on JWT Token Expired Or not.

```java
String userName = "Zaid Bashir";
Dynatrace().identifyUser(userName);

```

1. **Exit Session** :

To Exit the current session, just add below code snippet in your logout functionality in the App.

```java
Dynatrace().endSession();

```

Thank you for reading this guide on monitoring Flutter apps with Dynatrace! We hope it helps you achieve seamless end-to-end observability for your applications. Happy monitoring! 🚀.
