Sobes.tech
Junior — Middle

What state management mechanism would you use for developing a new application from scratch?

sobes.tech AI

Answer from AI

For a new Flutter application from scratch, I would choose a modern and widely supported state management mechanism, such as Provider or Riverpod.

  • Provider — a simple and lightweight way to inject dependencies and manage state, well integrated with Flutter, suitable for most applications.

  • Riverpod — a more modern and safer alternative to Provider with improved architecture and testing capabilities.

If the application is complex and requires more scalable state management, you can consider Bloc or Cubit, which provide a clear separation of business logic and UI.

The choice depends on the application's requirements, but for starting out, Provider or Riverpod offer an optimal balance of simplicity and functionality.

Example of using Provider:

class Counter with ChangeNotifier {
  int _count = 0;
  int get count => _count;

  void increment() {
    _count++;
    notifyListeners();
  }
}

// In main.dart
void main() {
  runApp(
    ChangeNotifierProvider(
      create: (_) => Counter(),
      child: MyApp(),
    ),
  );
}