0

In the docs from ScopedModel there is these instructions to retrieve a ScopedModel previosly created without ScopedModelDescendant. I don't realize if I could get rid from ScopedModelDescendant at all or I should declare it somewhere at last once?

From the docs:

Finding the Model #

There are two ways to find the Model provided by the ScopedModel Widget.

Use the ScopedModelDescendant Widget. It will find the Model and run the builder function whenever the Model notifies the listeners.
Use the ScopedModel.of static method directly. To make this method more readable for frequent access, you can consider adding your own of

method to your own Model classes like so:

class CounterModel extends Model { // ... /// Wraps [ScopedModel.of] for this [Model]. static CounterModel of(BuildContext context) => ScopedModel.of(context); }

I wouldn't use ScopedModelDescendant, so I have instantiated the ScopedModel in the root:

@override
  Widget build(BuildContext context) {
    return ScopedModel<TeamModel>(
            model: TeamModel(widget._team),
            child: Container(
    ...
    ))

And I created a static method of as shown in the docs:

class TeamModel extends Model {


  static TeamModel of(BuildContext context){
    var of = ScopedModel.of<TeamModel>(context);
    return of;
  }
}

But I cannot retrieve the model inside my Container, it gives that famous error message:

22:38:41.603 11 info flutter.tools I/flutter ( 9749): Error: Could not find the correct ScopedModel.
22:38:41.603 12 info flutter.tools I/flutter ( 9749):     
22:38:41.603 13 info flutter.tools I/flutter ( 9749): To fix, please:
22:38:41.603 14 info flutter.tools I/flutter ( 9749):           
22:38:41.604 15 info flutter.tools I/flutter ( 9749):   * Provide types to ScopedModel<MyModel>
22:38:41.604 16 info flutter.tools I/flutter ( 9749):   * Provide types to ScopedModelDescendant<MyModel> 
22:38:41.604 17 info flutter.tools I/flutter ( 9749):   * Provide types to ScopedModel.of<MyModel>() 
22:38:41.604 18 info flutter.tools I/flutter ( 9749):   * Always use package imports. Ex: `import 'package:my_app/my_model.dart';

It's not clear for me from the docs If I should use ScopedModelDescendant or else what is the problem with my code?

alexpfx
  • 6,412
  • 12
  • 52
  • 88

0 Answers0