错误:使用ScopedModel.of <mymodel>(上下文)时找不到正确的ScopedModel

时间:2019-04-26 07:13:45

标签: dart flutter scoped-model

作为局外人研究波动状态管理,我喜欢按照flutter.dev中的建议使用scoped_model进行状态管理的想法。我重写了正在运行的计数器应用程序。我可以使用ScopedModelDescendant<CounterModel>访问模型属性,但是在使用ScopedModel.of<CounterModel>(context)访问模型属性时遇到麻烦。有人可以建议我可能做错了什么吗?我有一种预感,那就是ScopedModel在我的小部件树中的位置。我的代码和错误消息如下。

main.dart

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';
import 'package:second/model/counter_model.dart';

void main() => runApp(MyApp());

class MyApp extends StatelessWidget {
  // This widget is the root of your application.
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: MyHomePage(),
    );
  }
}

class MyHomePage extends StatefulWidget {
  @override
  _MyHomePageState createState() => _MyHomePageState();
}

class _MyHomePageState extends State<MyHomePage> {
  Widget build(context) {
    return ScopedModel(
      model: new CounterModel(),
      child: Scaffold(
          appBar: AppBar(
            title: Text('ScopedModel'),
          ),
          body: ScopedModelDescendant<CounterModel>(
            builder: (context, child, value) {
              return Text("Pressed ${value.counter} times");
            },
          ),
          floatingActionButton: buildFab1()),
    );
  }

  Widget buildFab1() {
    return ScopedModelDescendant<CounterModel>(
      builder: (context, child, model) => FloatingActionButton(
            child: Icon(Icons.add),
            onPressed: model.incrementCounter,
          ),
    );
  }

  Widget buildFab2(BuildContext context) {
    return FloatingActionButton(
      child: Icon(Icons.add),
      onPressed: ScopedModel.of<CounterModel>(context).incrementCounter,
    );
  }
}

model / counter_model.dart

import 'package:scoped_model/scoped_model.dart';

class CounterModel extends Model {
  int _counter = 0;

  int get counter => _counter;

  void incrementCounter() {
    _counter++;
    notifyListeners();
  }
}

main.dart中,如果我使用buildFab2(context)而不是buildFab1(),则会出现以下错误


flutter: The following ScopedModelError was thrown building ScopedModelDescendant<Model>(dirty):
flutter: Error: Could not find the correct ScopedModel.
flutter:
flutter: To fix, please:
flutter:
flutter:   * Provide types to ScopedModel<MyModel>
flutter:   * Provide types to ScopedModelDescendant<MyModel>
flutter:   * Provide types to ScopedModel.of<MyModel>()
flutter:   * Always use package imports. Ex: `import 'package:my_app/my_model.dart';
flutter:

我看了一些SO问题,但是没有一个帮助。

2 个答案:

答案 0 :(得分:1)

是的,因为您通过的context不会有ScopedModel的{​​{1}}。

您可以做的是将CounterModel包装在buildFab2小部件中,该小部件将为您提供Builder,其中contextScopedModel为父。 / p>

赞:

CounterModel

您的应用将如下所示:

Builder(
    builder: (context){
      return buildFab2(context);
    },
  )

希望有帮助!

答案 1 :(得分:0)

您需要像这样在 main.dart 中添加 scoppedmodel:

@override
  Widget build(BuildContext context){
    return ScopedModel<UserModel>(
        model: UserModel(),
        child: MaterialApp(
            title: "Quiz Flamengo",
            debugShowCheckedModeBanner: false,
            home: Scaffold(
              body: HomeScreen(),
            )
        )
    );
  }