1

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:

  1. Detect BLE status (on/off/permissions etc.)
  2. Device connection status (BLE)
  3. Device connection status (USB)
  4. 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

RealRK
  • 315
  • 3
  • 19
  • Provider isn't great during that scenario. It has a different use in state management. It helps you Provide data to many screens without passing that data around. It would be better to use Bloc State management which uses Streams to update the widget's data. I would recommend learning (streams and rxdart) or flutter_bloc. I would suggest compiling all your data that your screen needs into a model/class/its own type and provide that data to your widgets so you use only one stream to update all of your widgets. – CoderUni Jan 05 '21 at 14:53
  • 1
    Have a look at https://medium.com/flutter-community/why-use-rxdart-and-how-we-can-use-with-bloc-pattern-in-flutter-a64ca2c7c52d and this is what I mean about model (scroll down to class User): https://stackoverflow.com/questions/58146158/using-model-class-in-flutter-dart – CoderUni Jan 05 '21 at 14:56
  • 1
    you should use `riverpod`. basically it's the same as provider but all the features you want is easier to be implemented in `riverpod`. – Mohammed Alfateh Jan 05 '21 at 15:24

0 Answers0