custom code fails Setting up a custom action to subscribe to mqtt topics and set app state variable

I am new to flutterflow. I have sensor readings (temperature, humidity...) that send out mqtt messages I want my FlutterFlow app to subscribe and show.

I decided to run an action on a page load. The challenge I am having is with the mqtt_client library. From what I have read (and the error I received), I need to use MqttServerClient class to connect with my broker. My challenge is the code compiler tells me MqttServerClient cannot be resolved. However, I have added the pub.dev mqtt_client^10.0.0:

// Automatic FlutterFlow imports
import '/flutter_flow/flutter_flow_theme.dart';
import '/flutter_flow/flutter_flow_util.dart';
import '/custom_code/actions/index.dart'; // Imports other custom actions
import '/flutter_flow/custom_functions.dart'; // Imports custom functions
import 'package:flutter/material.dart';
// Begin custom action code
// DO NOT REMOVE OR MODIFY THE CODE ABOVE!

import 'package:mqtt_client/mqtt_client.dart';

Future listenMqttAction() async {
  // Create a new mqtt client instance
  final client = MqttServerClient('192.168.68.118', 'client_id'); //DOES NOT RESOLVE.

  // Connect to the mqtt server
  await client.connect();

  // Subscribe to the mqtt topic
  client.subscribe('snifferbuddy/reading', MqttQos.atLeastOnce);
  // Listen for incoming mqtt messages
  client.updates?.listen((List<MqttReceivedMessage<MqttMessage>> messages) {
    // Loop through each message
    messages.forEach((MqttReceivedMessage<MqttMessage> message) {
      final payload = message.payload.toString();
      // Update the app state variable with the message payload
      var appstate = FFAppState();
      appstate.update(() {
        appstate.mm = payload;
      });
    });
  });
}
```
I am at a loss why MqttServerClient can't resolve when I am including the mqtt_client.dart package.  help appreciated.  Thank you.
1
1 reply