Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
455 views
in Technique[技术] by (71.8m points)

flutter - Expert State Management advice needed

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


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)
等待大神答复

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share
...