gpt4 book ai didi

flutter - Flutter 中的 notifyListeners 无法正常工作

转载 作者:行者123 更新时间:2023-12-03 02:48:16 25 4
gpt4 key购买 nike

我有一个调用 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) {...}
),
);
}
}
因此,当我调用通知时,正如预期的那样,{...} 中的任何内容都应该重建,但它没有。

最佳答案

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

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),
),
);
});
}
}
此外,我很好奇为什么需要使用 scope_model stateful and stateless widgets 时更新屏幕上的小部件可以轻松更新。

关于flutter - Flutter 中的 notifyListeners 无法正常工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56837928/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com