【问题标题】:notifyListeners in Flutter not working correctlyFlutter 中的 notifyListeners 无法正常工作
【发布时间】:2019-07-01 14:58:12
【问题描述】:

我在构建中有一个调用 notifyListeners 的模型和一个 ScopedModelDescendant 小部件,即使它 100% 调用 notifyListeners(),它也不会更新构建。

我查看了它,似乎模型在 _listeners 中没有任何内容。

class MyModel extends Model {

  void notify() {
    notifyListeners();
  }

}

class Main extends StatefulWidget {

  _MainState createState() => _MainState();

}

class _MainState extends State<StatefulWidget> {

  Widget build(BuildContext context) {
    return ScopedModel<MyModel>(
      model: MyModel(),
      child: ScopedModelDescendant<MyModel>(
        builder: (context, child, model) {...}
      ),
    );
  }
}

所以当我调用通知时,正如预期的那样,{...} 中的任何内容都应该重建,但它没有。

【问题讨论】:

  • 您是否使用 model.notify() 在 ScopedModelDescendant 中调用了 notify() ?
  • 检查ScopedModelDescendant构造函数参数
  • @Daniyar 我在另一个构建中调用它,这只是简单的代码,但我没有在 ScopedModelDescendant 中调用它,这是肯定的
  • @pskink ScopedModelDescendant 的一般构造函数还是我的模型的构造函数是什么意思?
  • 我的意思是ScopedModelDescendant小部件类的构造函数

标签: flutter dart


【解决方案1】:

我尝试使用scoped_model 中提供的sample 复制此行为,但屏幕按预期更新。我对示例进行了一些修改以匹配您的部分代码。你可以试试这个。

import 'package:flutter/material.dart';
import 'package:scoped_model/scoped_model.dart';

void main() {
  runApp(MyApp(
    model: CounterModel(),
  ));
}

class MyApp extends StatelessWidget {
  final CounterModel model;

  const MyApp({Key key, @required this.model}) : super(key: key);

  @override
  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<CounterModel>(
      model: model,
      child: MaterialApp(
        title: 'Scoped Model Demo',
        home: CounterHome('Scoped Model Demo'),
      ),
    );
  }
}

// Start by creating a class that has a counter and a method to increment it.
//
// Note: It must extend from Model.
class CounterModel extends Model {
  int _counter = 0;

  int get counter => _counter;

  void increment() {
    // First, increment the counter
    _counter++;

    // Then notify all the listeners.
    notifyListeners();
  }
}

class CounterHome extends StatelessWidget {
  final String title;

  CounterHome(this.title);

  @override
  Widget build(BuildContext context) {
    return ScopedModelDescendant<CounterModel>(
        builder: (context, child, model) {
      return Scaffold(
        appBar: AppBar(
          title: Text(title),
        ),
        body: Center(
          child: Column(
            mainAxisAlignment: MainAxisAlignment.center,
            children: <Widget>[
              Text('You have pushed the button this many times:'),
              // Create a ScopedModelDescendant. This widget will get the
              // CounterModel from the nearest parent ScopedModel<CounterModel>.
              // It will hand that CounterModel to our builder method, and
              // rebuild any time the CounterModel changes (i.e. after we
              // `notifyListeners` in the Model).
              Text(
                model.counter.toString(),
                style: Theme.of(context).textTheme.display1,
              ),
            ],
          ),
        ),
        // Use the ScopedModelDescendant again in order to use the increment
        // method from the CounterModel
        floatingActionButton: FloatingActionButton(
          onPressed: model.increment,
          tooltip: 'Increment',
          child: Icon(Icons.add),
        ),
      );
    });
  }
}

此外,我很好奇为什么在stateful and stateless widgets 可以轻松更新的情况下需要使用scope_model 来更新屏幕上的小部件。

【讨论】:

    猜你喜欢
    • 2021-08-22
    • 2021-05-17
    • 2021-09-04
    • 1970-01-01
    • 2023-01-04
    • 2021-06-11
    • 2018-09-27
    • 2021-05-28
    • 1970-01-01
    相关资源
    最近更新 更多