Sobes.tech
Junior

What is Scoped Model used for in Flutter?

sobes.tech AI

Answer from AI

Scoped Model is a simple library for state management in Flutter, based on the concept of InheritedWidget. It is designed to provide access to data (model) and the ability to modify it to widgets in the subtree without the need to explicitly pass data through constructors at each level.

Main components of Scoped Model:

  • Model: A class that stores data and business logic. It should extend Model from the scoped_model package.
  • ScopedModel: A widget that wraps a part of the widget tree. It takes an instance of Model and makes it available to itself and its descendants.
  • ScopedModelDescendant: A widget that allows access to the Model instance from the nearest ScopedModel above in the tree and subscribes to its changes.

Advantages of using Scoped Model:

  • Simplicity: Easy to understand and use, especially for small and medium-sized applications.
  • Based on standard Flutter mechanisms: Uses InheritedWidget, making it performant.
  • Well documented: Comprehensive documentation and examples are available.

Example of usage:

  1. Create a model:

    import 'package:scoped_model/scoped_model.dart';
    
    class CounterModel extends Model {
      int _counter = 0;
    
      int get counter => _counter;
    
      void increment() {
        _counter++;
        notifyListeners(); // Notify widgets about the change
      }
    }
    
  2. Wrap part of the tree in ScopedModel:

    import 'package:flutter/material.dart';
    import 'package:scoped_model/scoped_model.dart';
    import 'counter_model.dart'; // Import the model
    
    void main() {
      runApp(
        // Wrap our app in ScopedModel
        ScopedModel<CounterModel>(
          model: CounterModel(),
          child: MyApp(),
        ),
      );
    }
    
    class MyApp extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        return MaterialApp(
          home: Scaffold(
            appBar: AppBar(title: Text('Scoped Model Example')),
            body: Center(
              child: CounterDisplay(),
            ),
            floatingActionButton: IncrementButton(),
          ),
        );
      }
    }
    
  3. Access the model in ScopedModelDescendant:

    import 'package:flutter/material.dart';
    import 'package:scoped_model/scoped_model.dart';
    import 'counter_model.dart'; // Import the model
    
    class CounterDisplay extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // Access the model and subscribe to its changes
        return ScopedModelDescendant<CounterModel>(
          builder: (context, child, model) {
            return Text(
              'Counter: ${model.counter}',
              style: Theme.of(context).textTheme.headline4,
            );
          },
        );
      }
    }
    
    class IncrementButton extends StatelessWidget {
      @override
      Widget build(BuildContext context) {
        // Access the model without subscribing to changes
        return FloatingActionButton(
          onPressed: () {
            ScopedModel.of<CounterModel>(context).increment();
          },
          child: Icon(Icons.add),
        );
      }
    }
    

Scoped Model is well-suited for applications where state management is not too complex and can do without more powerful but verbose solutions like BLoC or Riverpod. However, for large and complex applications with many interconnected states, more advanced approaches may be necessary.

What is Scoped Model used for in Flutter? — Flutter - sobes.tech