我建议使用 Provider 并创建一个RPIService
发送连接状态流的。
class RPIService {
var RPIstatus = ValueNotifier<Status>(Status('offline'));
RPIService(){
rpi...listen((cstatus){
RPIstatus.add(Status(cstatus));
});
}}
class Status {
final String statusMessage;
Status(this.statusMessage);
}
主要.dart
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
Provider<UserService>(
create: (context) => UserService(),
lazy: false,
),
StreamProvider<Status>(
create: (context) =>
Provider.of<RPIService>(context, listen: false).RPIstatus,
),
],
child: MaterialApp(
title: 'Your app',
theme: ThemeData(
primarySwatch: Colors.blue,
),
home: YourHome(),
),
);
}
}
你的家.dart
class YourHome extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<Status>(builder: (context, status, _) {
return Scaffold(
body: Text(status.status),
);
});
}
}