I'm using the Provider package to push data and make changes to the state through the data which changes inside this. My app uses Bluetooth and USB Serial connections since it manages IoT devices.
Here is the main.dart file:
void main() async {
WidgetsFlutterBinding.ensureInitialized();
SystemChrome.setEnabledSystemUIOverlays([]);
SystemChrome.setPreferredOrientations([
DeviceOrientation.portraitUp,
DeviceOrientation.portraitDown,
]);
await Firebase.initializeApp();
final _ble = FlutterReactiveBle();
final _scanner = BleScanner(_ble);
final _monitor = BleStatusMonitor(_ble);
final _connector = BleDeviceConnector(_ble);
runApp(
MultiProvider(
providers: [
ChangeNotifierProvider(create: (_) => USBManager()),
StreamProvider<User>.value(
value: AuthService().user,
),
Provider.value(value: _scanner),
Provider.value(value: _monitor),
Provider.value(value: _connector),
StreamProvider<BleScannerState>(
create: (_) => _scanner.state,
initialData: const BleScannerState(
discoveredDevices: [],
scanIsInProgress: false,
),
),
StreamProvider<BleStatus>(
create: (_) => _monitor.state,
initialData: BleStatus.unknown,
),
StreamProvider<ConnectionStateUpdate>(
create: (_) => _connector.state,
initialData: const ConnectionStateUpdate(
deviceId: 'Unknown device',
connectionState: DeviceConnectionState.disconnected,
failure: null,
),
),
],
child: MaterialApp(
debugShowCheckedModeBanner: false,
navigatorObservers: [
FirebaseAnalyticsObserver(analytics: FirebaseAnalytics()),
],
routes: {
'/': (context) => RegisterScreen(),
'/navBar': (context) => AppBottomNav(),
'/scan': (context) => ScanScreen(),
},
theme: ThemeData(
bottomAppBarTheme: BottomAppBarTheme(
color: Colors.green[400],
),
brightness: Brightness.dark,
primarySwatch: Colors.green,
visualDensity: VisualDensity.adaptivePlatformDensity),
),
),
);
}
As you can see, there are many streams that need to be listened to simultaneously to:
- Detect BLE status (on/off/permissions etc.)
- Device connection status (BLE)
- Device connection status (USB)
- Firebase User
In addition to this, I want to prevent the user from accessing certain pages of the app if the connection gets abruptly disconnected and many other state changes.
My Question: Since there are so many instances of so many classes running and I want to access the active instances of the classes to read streams and make the classes interact with each other (detect sudden disconnects, successful connection and much more, which will make a change in the changenotifier and update the UI), is provider the best solution? Should I run all the device connection instances in one Change Notifier provider and access that only to make the work easier?
Is there a way of making multiple providers interact with each other?
Thanks in advance