Closed ashleshp closed 3 years ago
The client doesn't provide a mechanism for auto connecting on start, you have to create the client and call connect from your code.
From the log output above -
E/flutter (31363): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'setAppConnectionState' was called on null.
This error is output by flutter, and would seem to be in your code, not the client.
As we get appstate once the build is done, Iam not able to call it in "initstate" I have tried asynchronous calls but dont know if its right or wrong . Can you suggest me any idea?
I'm not a flutter user so I wouldn't know, you are better off asking on a flutter list.
Suppose we have a function configureandconnect which connects client to any mqtt endpoint we can call this function in initstate by using PostFrameCallback: `void initState() { super.initState(); WidgetsBinding.instance.addPostFrameCallback((_) => currentAppState.getAppConnectionState==MQTTAppConnectionState.disconnected? _configureAndConnect():null);
}`
IS there a way where we can auto connect to endpoint on app start??? I have tried this and these were my logs**
`class _MQTTViewState extends State {
final TextEditingController _hostTextController = TextEditingController();
final TextEditingController _messageTextController = TextEditingController();
final TextEditingController _topicTextController = TextEditingController();
MQTTAppState currentAppState;
MQTTServerClientManager manager;
final String demoAWSIOTEndpoint =
'a1vh8wv3c2znzr.ats.iot.cn-north-1.amazonaws.com.cn';
final String demoAWSIOTTopic = 'drivingCycleTopic';
final String demoText =
'{"message":"hello from android/ios"}';
@override void initState() { super.initState(); _configureAndConnect(); }
@override void dispose() { _hostTextController.dispose(); _messageTextController.dispose(); _topicTextController.dispose(); super.dispose(); }
@override Widget build(BuildContext context) { final MQTTAppState appState = Provider.of(context);
// Keep a reference to the app state.
currentAppState = appState;
final Scaffold scaffold =
Scaffold(appBar: _buildAppBar(context), body: _buildColumn());
return scaffold;
}
void _configureAndConnect() {
// TODO: Use UUID
String osPrefix = 'Flutter_iOS';
if (Platform.isAndroid) {
osPrefix = 'Flutter_Android';
}
manager = MQTTServerClientManager(
host: "a2qjty4wsdd1fv-ats.iot.ap-south-1.amazonaws.com",
topic: _topicTextController.text,
//host: demoAWSIOTEndpoint,
//topic: demoAWSIOTTopic,
identifier: osPrefix,
state: currentAppState);
manager.initializeMQTTClient();
manager.connect();
}
and my logs were I/flutter (31363): EXAMPLE::Mosquitto start client connecting.... E/flutter (31363): [ERROR:flutter/lib/ui/ui_dart_state.cc(186)] Unhandled Exception: NoSuchMethodError: The method 'setAppConnectionState' was called on null. E/flutter (31363): Receiver: null E/flutter (31363): Tried calling: setAppConnectionState(Instance of 'MQTTAppConnectionState') E/flutter (31363): #0 Object.noSuchMethod (dart:core-patch/object_patch.dart:54:5) E/flutter (31363): #1 MQTTServerClientManager.connect (package:flutter_mqtt_aws_iot/mqtt/state/MQTTServerClientManager.dart:72:21) E/flutter (31363): #2 _MQTTViewState._configureAndConnect (package:flutter_mqtt_aws_iot/widgets/mqttView.dart:263:13) E/flutter (31363): #3 _MQTTViewState.initState (package:flutter_mqtt_aws_iot/widgets/mqttView.dart:35:5) E/flutter (31363): #4 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4632:57) E/flutter (31363): #5 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4469:5) E/flutter (31363): #6 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #7 Element.updateChild (package:flutter/src/widgets/framework.dart:3306:18) E/flutter (31363): #8 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4520:16) E/flutter (31363): #9 _InheritedProviderScopeElement.performRebuild (package:provider/src/inherited_provider.dart:426:11) E/flutter (31363): #10 Element.rebuild (package:flutter/src/widgets/framework.dart:4189:5) E/flutter (31363): #11 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4474:5) E/flutter (31363): #12 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4469:5) E/flutter (31363): #13 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #14 Element.updateChild (package:flutter/src/widgets/framework.dart:3306:18) E/flutter (31363): #15 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4520:16) E/flutter (31363): #16 Element.rebuild (package:flutter/src/widgets/framework.dart:4189:5) E/flutter (31363): #17 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4474:5) E/flutter (31363): #18 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4469:5) E/flutter (31363): #19 SingleChildWidgetElementMixin.mount (package:nested/nested.dart:222:11) E/flutter (31363): #20 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #21 Element.updateChild (package:flutter/src/widgets/framework.dart:3306:18) E/flutter (31363): #22 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5974:14) E/flutter (31363): #23 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #24 Element.updateChild (package:flutter/src/widgets/framework.dart:3306:18) E/flutter (31363): #25 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4520:16) E/flutter (31363): #26 Element.rebuild (package:flutter/src/widgets/framework.dart:4189:5) E/flutter (31363): #27 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4474:5) E/flutter (31363): #28 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4469:5) E/flutter (31363): #29 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #30 Element.updateChild (package:flutter/src/widgets/framework.dart:3306:18) E/flutter (31363): #31 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5974:14) E/flutter (31363): #32 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #33 Element.updateChild (package:flutter/src/widgets/framework.dart:3306:18) E/flutter (31363): #34 SingleChildRenderObjectElement.mount (package:flutter/src/widgets/framework.dart:5974:14) E/flutter (31363): #35 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #36 Element.updateChild (package:flutter/src/widgets/framework.dart:3306:18) E/flutter (31363): #37 ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4520:16) E/flutter (31363): #38 StatefulElement.performRebuild (package:flutter/src/widgets/framework.dart:4667:11) E/flutter (31363): #39 Element.rebuild (package:flutter/src/widgets/framework.dart:4189:5) E/flutter (31363): #40 ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4474:5) E/flutter (31363): #41 StatefulElement._firstBuild (package:flutter/src/widgets/framework.dart:4658:11) E/flutter (31363): #42 ComponentElement.mount (package:flutter/src/widgets/framework.dart:4469:5) E/flutter (31363): #43 Element.inflateWidget (package:flutter/src/widgets/framework.dart:3541:14) E/flutter (31363): #44 Element.u I/flutter (31363): EXAMPLE::Mosquitto client connecting....`