颤振错误:正文可能正常完成,导致返回'null'



我从 MQTT 代理返回获取数据并在变量中设置数据。我遇到了这个问题,我可以知道为什么吗?

void main() {
runApp(MaterialApp(home: MyApp()));
}
class MyApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
debugShowCheckedModeBanner: true,
title: 'Flutter Demo',
theme: ThemeData(
primarySwatch: Colors.blue,
visualDensity: VisualDensity.adaptivePlatformDensity,
),
home: sensor01(),
);
}
}
class sensor01 extends StatefulWidget {
const sensor01({Key? key}) : super(key: key);
@override
State<sensor01> createState() => _sensor01();
}
class _sensor01 extends State<sensor01> {
// connection succeeded
void onConnected() {
print('Connected');
}
// unconnected
void onDisconnected() {
print('Disconnected');
}
// subscribe to topic succeeded
void onSubscribed(String topic) {
print('Subscribed topic: $topic');
}
// subscribe to topic failed
void onSubscribeFail(String topic) {
print('Failed to subscribe $topic');
}
// unsubscribe succeeded
void onUnsubscribed(String topic) {
print('Unsubscribed topic: $topic');
}
// PING response received
void pong() {
print('Ping response client callback invoked');
}
var data;
Future<MqttServerClient> connect() async {
MqttServerClient client = MqttServerClient.withPort(host, id, port);
client.logging(on: false);
client.onConnected = onConnected;
client.onDisconnected = onDisconnected;
// client.onUnsubscribed = onUnsubscribed;
client.onSubscribed = onSubscribed;
client.onSubscribeFail = onSubscribeFail;
client.pongCallback = pong;
client.autoReconnect = false;
final connMessage = MqttConnectMessage()
.authenticateAs(username, password)
.withClientIdentifier(id)
.startClean()
// .withWillRetain()
.withWillQos(MqttQos.atLeastOnce);
client.connectionMessage = connMessage;
try {
await client.connect();
// client.unsubscribe('topic/');
client.subscribe(topic1, MqttQos.atLeastOnce);
} catch (e) {
print('Exception: $e');
client.disconnect();
}
client.updates!.listen((List<MqttReceivedMessage<MqttMessage>> c) {
final MqttPublishMessage message = c[0].payload as MqttPublishMessage;
final payload =
MqttPublishPayload.bytesToStringAsString(message.payload.message);
print('Received message:$payload from topic: ${c[0].topic}>');
Map<String, dynamic> userMap = jsonDecode(payload);
var user = dataList.fromJson(userMap);
setState(() {
if (user.sensorid == 'sensor01') {
data = user.sensorid;
}
});
});
@override
void initState() {
super.initState();
connect();
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text("MQTT"),
),
body: FutureBuilder(
future: connect(),
builder: (BuildContext context, AsyncSnapshot snapshot) {
if (snapshot.hasError) {
return Center(
child: Text("Error: ${snapshot.error}"),
);
}
// if succeed to connect
if (snapshot.connectionState == ConnectionState.done) {
return ListView(
children: [
Card(
child: ListTile(
title: Text(data),
))
],
padding: EdgeInsets.all(10),
);
}
return Center(child: CircularProgressIndicator());
},
));
}
}
@override
Widget build(BuildContext context) {
// TODO: implement build
throw UnimplementedError();
}
}

我得到了这个错误。该错误显示在覆盖小部件上方的 Fure 函数中。我应该添加什么才能使代码能够运行,即使返回的数据为 null?

The body might complete normally, causing 'null' to be returned, but the return type, 'FutureOr<MqttServerClient>', is a potentially non-nullable type.

如何解决错误??

在你的函数connect()中,你的返回类型是MqttServerClient

Future<MqttServerClient> connect() async {}

但你什么都没return。 你的函数不返回任何内容 - 它void.

因此,更改:

Future<MqttServerClient> connect() async {}

自:

Future<void> connect() async {}

最新更新