Flutter-提供程序未更新处于加载屏幕的状态

时间:2020-02-21 06:44:29

标签: flutter dart widget mqtt flutter-layout

我需要专家意见。 我是新手,我似乎无法解决这个问题。

这是我的问题:

我正在构建一个将连接到MQTT的应用程序。 订阅主题。说主题A。 在那之后,它将发布一些话题B。 然后它将在主题A中收到一些说明。

因此,当它初始化MQTT并经历连接过程时,我想显示“加载屏幕”。 完成连接后,我只想显示一个文本。

这是我的提供程序处理程序;

import 'package:dnb/screens/loading_screen.dart';
import 'package:dnb/screens/main_screen.dart';
import 'package:flutter/material.dart';
enum MQTTAppConnectionState { connected, disconnected, connecting }
class MQTTAppState with ChangeNotifier {
  MQTTAppConnectionState _appConnectionState =
      MQTTAppConnectionState.disconnected;
  void setAppConnectionState(MQTTAppConnectionState state) {
    _appConnectionState = state;
    notifyListeners();
  }
  MQTTAppConnectionState get getAppConnectionState => _appConnectionState;
  Widget get getRoute {
    return _appConnectionState == MQTTAppConnectionState.connected
        ? MainScreen()
        : LoadingScreen();
  }
}

然后这是我的main.dart:

import 'package:dnb/services/MQTTAppState.dart';
import 'package:dnb/services/MQTTService.dart';
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
void main() => runApp(DigitalNoticeBoard());
class DigitalNoticeBoard extends StatefulWidget {
  @override
  _DigitalNoticeBoardState createState() => _DigitalNoticeBoardState();
}
class _DigitalNoticeBoardState extends State<DigitalNoticeBoard> {
  @override
  void initState() {
    mqttService();
    super.initState();
  }
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      theme: ThemeData.dark(),
      home: ChangeNotifierProvider(
        create: (_) => MQTTAppState(),
        child: Scaffold(
          body: Consumer<MQTTAppState>(
              builder: (_, mqttAppState, __) => mqttAppState.getRoute),
        ),
      ),
    );
  }
}

正如您在main.dart中看到的那样,我正在initState()中启动mqttService()。当我运行代码时,这就是我得到的:

Performing hot restart...
Syncing files to device POCOPHONE F1...
Restarted application in 1,587ms.
I/flutter (16925): ::Mosquitto client connecting....
E/AccessibilityBridge(16925): VirtualView node must not be the root node.
I/flutter (16925): ::OnConnected client callback - Client connection was sucessful
I/flutter (16925): ::Mosquitto client connected
I/flutter (16925): ::Subscription confirmed -> topic -> topicName
I/flutter (16925): ::Change notification:: topic -> <topicName>, payload ->  <00010000011000000000> 

此预期输出,但我的应用程序停留在加载屏幕中。打印客户端连接我的应用程序的那一刻,应从加载屏幕移至主屏幕。 这就是我在mqttService()中所做的:

try {
  mqttAppState.setAppConnectionState(MQTTAppConnectionState.connecting); //Passing connecting
  await client.connect();
} on Exception catch (e) {
  print('::client exception - $e');
  //client.disconnect();
}

然后:

/// Check we are connected
if (client.connectionStatus.state == MqttConnectionState.connected) {
  mqttAppState.setAppConnectionState(MQTTAppConnectionState.connected); //Passing connected
  print('::Mosquitto client connected');
} else {
  /// Use status here rather than state if you also want the broker return code.
  print(
      '::ERROR Mosquitto client connection failed - disconnecting, status is ${client.connectionStatus}');
  client.disconnect();
}

1 个答案:

答案 0 :(得分:0)

@Momshad Dinury

mqttService()

try {
  mqttAppState.setAppConnectionState(MQTTAppConnectionState.connecting); //Passing connecting
  await client.connect();
} on Exception catch (e) {
  print('::client exception - $e');
  //client.disconnect();
}

此功能上的 mqttAppState 是什么?

我认为 create中的 MQTTAppState :(_)=> MQTTAppState()与您的 mqttAppState 不同。

您可以尝试以下建议:

MQTTAppState mqttAppState;

  @override
  void initState() {
    mqttAppState = MQTTAppState();
    mqttService();
    super.initState();
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: ChangeNotifierProvider.value(
        value: mqttAppState,
        child: Scaffold(
          body: Consumer<MQTTAppState>(
              builder: (_, mqttAppState, __) => mqttAppState.getRoute),
        ),
      ),
    );
  }