Flutter Using Multiple Models with Scoped Model

2019-07-08 10:23发布

问题:

I'm trying to build an Expense tracker App in Flutter and has decided to use Scoped Model for state management. The App has a User who can have many Accounts and each account can have many Transactions.

How do I model these to be used with Scoped Model, I am stuck on selecting a good Architecture.

If I create a UserModel that has a list of Accounts where each Account is an AccountModel then triggering and update from inside AccountModel would not trigger the ones accessing the UserModel class.

回答1:

class CombinedWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    final username =
      ScopedModel.of<UserModel>(context, rebuildOnChange: true).username;
    final counter =
      ScopedModel.of<CounterModel>(context, rebuildOnChange: true).counter;

    return Text('$username tapped the button $counter times');

  }
}

you can see complete example in source .



回答2:

Let's start with a main question. Is the scoped model an MVVM? My answer is yes, it must be used as such if the entities have to be saved somewhere.

So, I would like to answer you well after using it for two months. In my opinion there are two possibilities that could be more abstract without having to recreate the structure of the models and one, that I have seen used, to be excluded if you don't want to run into data management problems between views.

The first is the classic:

Widget build(BuildContext context) {
// At the top level of our app, we'll, create a ScopedModel Widget. This
// will provide the CounterModel to all children in the app that request it
// using a ScopedModelDescendant.
return ScopedModel<UserModel>( // <========
  model: userModel,
  child: ScopedModel<CounterModel>( // <========
    model: counterModel,
    child: MaterialApp(
      title: 'Scoped Model Demo',
      home: CounterHome('Scoped Model Demo'),
    ),
  ),
);

} }

The second most intelligent method in my opinion is that of loading the most important models globally, and those needed only for some views, such as a timer, to insert them at the highest level of the page to be used.

The second example is this: In main.dart

return ScopedModel<UserModel>(
  model: UserModel(),
  child: MaterialApp(
    theme: ThemeData(
      primaryColor: PrimaryColor,
    ),
    home: new SplashScreen2(),
  ),
);

In workout.dart:

return ScopedModel<TimerModel>(

model: TimerModel(), child: Scaffold( child:Center(_widget) ), );

In your _widget, now, you can using a ScopedModelDescendant of UserModel and of the TimerModel..

What I don't advise you to do is create a main model with the mix and extend all the other models. Unfortunately I saw some videos that do this, without explaining the consequences. Or, if you want to do it, make sure there are no links between them, because this could create confusion between what should really be updated or not I advise you to use this solution only if you have 2-3 models.

I hope I was helpful to someone who was wondering how to use it