如何在 Flutter 中使用来自 Provider 的嵌套子项通知 Listeners?
How to notifyListeners with nested child from Provider in Flutter?
伙计们,
我从 flutter 开始,我决定制作一个像 "cookie clicker" 这样的迷你游戏来训练,但我 运行 遇到了状态管理问题...
我使用提供商来传播 "GameController" 旨在更新玩家的资源并进行计算。
我设法到处获得 "GameController",但是当我与资源("GameController" 中的对象)交互时,这个是最新的但不是 UI,它没有改变...
main.dart :
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'controllers/GameController.dart';
void main() => runApp(MokaOnline());
class MokaOnline extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => GameController()),
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _mokaResource =
gameControler.essentialResourceController.mokaResource;
return Container(
child: Column(
children: <Widget>[
Text('Moka : ${_mokaResource.getResource()}'),
FlatButton(
onPressed: () {
_mokaResource.increment();
},
child: Text('Add Moka'),
)
],
),
);
},
);
}
}
GameController.dart :
import 'package:flutter/foundation.dart';
import 'package:mokaonline/controllers/EssentialResourceController.dart';
class GameController {
EssentialResourceController _essentialResourceController =
EssentialResourceController();
EssentialResourceController get essentialResourceController =>
_essentialResourceController;
}
EssentialResourceController.dart :
import 'package:mokaonline/models/essentialResources/MokaResourceModel.dart';
import 'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';
class EssentialResourceController {
ResourceEssentialInterface _mokaResource = MokaResourceModel();
ResourceEssentialInterface get mokaResource => _mokaResource;
}
MokaResourceModel.dart :
import 'package:flutter/foundation.dart';
import 'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';
class MokaResourceModel extends ChangeNotifier
implements ResourceEssentialInterface {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
我知道 UI 没有刷新,因为它不是真正更新的 "GameController"...但是我不知道如何构建代码。
无论如何,感谢您的耐心等待,祝您愉快!
您正在寻找类似提供程序中的嵌套对象的东西
喜欢
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProxyProvider<MokaResourceModel, EssentialResourceController>(
create: (_) => EssentialResourceController(),
update: (_, moka, essR) => essR..mokaResourceModel=moka
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _mokaResource = gameControler.essentialResourceController.mokaResourceModel;
return new SafeArea(
child: Container(
child: Column(
children: <Widget>[
Text('Moka : ${_mokaResource.getResource()}'),
FlatButton(
onPressed: () {
_mokaResource.increment();
},
child: Text('Add Moka'),
)
],
),
),
);
},
);
}
}
class GameController extends ChangeNotifier {
GameController() {_EssentialResourceController = EssentialResourceController();}
EssentialResourceController _EssentialResourceController = EssentialResourceController();
EssentialResourceController get essentialResourceController => _EssentialResourceController;
set essentialResourceController(EssentialResourceController value)
{
_EssentialResourceController = value;
notifyListeners();
}
}
class EssentialResourceController extends ChangeNotifier {
EssentialResourceController() {_MokaResourceModel = MokaResourceModel();}
MokaResourceModel _MokaResourceModel = MokaResourceModel();
MokaResourceModel get mokaResourceModel => _MokaResourceModel;
set mokaResourceModel(MokaResourceModel value)
{
_MokaResourceModel = value;
notifyListeners();
}
}
class MokaResourceModel extends ChangeNotifier {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
有些地方不对,但也许你找到了错误
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProvider(create: (context) => WoodResource()),
ChangeNotifierProxyProvider2<MokaResourceModel, WoodResource, EssentialResourceController>(
create: (_) => EssentialResourceController(),
update: (_, moka, wood, essR) {
essR..mokaResourceModel=moka;
essR..woodResource=wood;
},
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _moka = gameControler.essentialResourceController.mokaResourceModel;
final _wood = gameControler.essentialResourceController.woodResource;
return new SafeArea(
child: Container(
child: Column(
children: <Widget>[
Text('Moka : ${_moka.getResource()}'),
FlatButton(
onPressed: () {
_moka.increment();
},
child: Text('Add Moka'),
),
new Container(height: 100.0,),
Text('Wood : ${_wood.getResource()}'),
FlatButton(
onPressed: () {
_wood.increment();
},
child: Text('Add Wood'),
)
],
),
),
);
},
);
}
}
class GameController extends ChangeNotifier {
GameController() {_EssentialResourceController = EssentialResourceController();}
EssentialResourceController _EssentialResourceController = EssentialResourceController();
EssentialResourceController get essentialResourceController => _EssentialResourceController;
set essentialResourceController(EssentialResourceController value)
{
_EssentialResourceController = value;
notifyListeners();
}
}
class EssentialResourceController extends ChangeNotifier {
EssentialResourceController() {
_MokaResourceModel = MokaResourceModel();
_WoodResource = WoodResource();
}
MokaResourceModel _MokaResourceModel = MokaResourceModel();
MokaResourceModel get mokaResourceModel => _MokaResourceModel;
set mokaResourceModel(MokaResourceModel value)
{
_MokaResourceModel = value;
notifyListeners();
}
WoodResource _WoodResource = WoodResource();
WoodResource get woodResource => _WoodResource;
set woodResource(WoodResource value)
{
_WoodResource = value;
notifyListeners();
}
}
class MokaResourceModel extends ChangeNotifier {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
class WoodResource extends ChangeNotifier {
int _wood = 0;
void increment() {
_wood++;
notifyListeners();
}
@override
int getResource() {
return _wood;
}
}
这样也好不了
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProvider(create: (context) => WoodResource()),
ChangeNotifierProxyProvider<MokaResourceModel, EssentialResourceController>(
key: UniqueKey(),
create: (_) => EssentialResourceController(),
update: (_, moka, essR) => essR..mokaResourceModel=moka
),
ChangeNotifierProxyProvider<WoodResource, EssentialResourceController>(
key: UniqueKey(),
create: (_) => EssentialResourceController(),
update: (_, wood, essR) => essR..woodResource=wood
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)
伙计们, 我从 flutter 开始,我决定制作一个像 "cookie clicker" 这样的迷你游戏来训练,但我 运行 遇到了状态管理问题...
我使用提供商来传播 "GameController" 旨在更新玩家的资源并进行计算。
我设法到处获得 "GameController",但是当我与资源("GameController" 中的对象)交互时,这个是最新的但不是 UI,它没有改变...
main.dart :
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'controllers/GameController.dart';
void main() => runApp(MokaOnline());
class MokaOnline extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => GameController()),
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _mokaResource =
gameControler.essentialResourceController.mokaResource;
return Container(
child: Column(
children: <Widget>[
Text('Moka : ${_mokaResource.getResource()}'),
FlatButton(
onPressed: () {
_mokaResource.increment();
},
child: Text('Add Moka'),
)
],
),
);
},
);
}
}
GameController.dart :
import 'package:flutter/foundation.dart';
import 'package:mokaonline/controllers/EssentialResourceController.dart';
class GameController {
EssentialResourceController _essentialResourceController =
EssentialResourceController();
EssentialResourceController get essentialResourceController =>
_essentialResourceController;
}
EssentialResourceController.dart :
import 'package:mokaonline/models/essentialResources/MokaResourceModel.dart';
import 'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';
class EssentialResourceController {
ResourceEssentialInterface _mokaResource = MokaResourceModel();
ResourceEssentialInterface get mokaResource => _mokaResource;
}
MokaResourceModel.dart :
import 'package:flutter/foundation.dart';
import 'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';
class MokaResourceModel extends ChangeNotifier
implements ResourceEssentialInterface {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
我知道 UI 没有刷新,因为它不是真正更新的 "GameController"...但是我不知道如何构建代码。
无论如何,感谢您的耐心等待,祝您愉快!
您正在寻找类似提供程序中的嵌套对象的东西
喜欢
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProxyProvider<MokaResourceModel, EssentialResourceController>(
create: (_) => EssentialResourceController(),
update: (_, moka, essR) => essR..mokaResourceModel=moka
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _mokaResource = gameControler.essentialResourceController.mokaResourceModel;
return new SafeArea(
child: Container(
child: Column(
children: <Widget>[
Text('Moka : ${_mokaResource.getResource()}'),
FlatButton(
onPressed: () {
_mokaResource.increment();
},
child: Text('Add Moka'),
)
],
),
),
);
},
);
}
}
class GameController extends ChangeNotifier {
GameController() {_EssentialResourceController = EssentialResourceController();}
EssentialResourceController _EssentialResourceController = EssentialResourceController();
EssentialResourceController get essentialResourceController => _EssentialResourceController;
set essentialResourceController(EssentialResourceController value)
{
_EssentialResourceController = value;
notifyListeners();
}
}
class EssentialResourceController extends ChangeNotifier {
EssentialResourceController() {_MokaResourceModel = MokaResourceModel();}
MokaResourceModel _MokaResourceModel = MokaResourceModel();
MokaResourceModel get mokaResourceModel => _MokaResourceModel;
set mokaResourceModel(MokaResourceModel value)
{
_MokaResourceModel = value;
notifyListeners();
}
}
class MokaResourceModel extends ChangeNotifier {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
有些地方不对,但也许你找到了错误
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:flutter/foundation.dart';
void main() => runApp(MyApp());
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MultiProvider(
providers: [
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProvider(create: (context) => WoodResource()),
ChangeNotifierProxyProvider2<MokaResourceModel, WoodResource, EssentialResourceController>(
create: (_) => EssentialResourceController(),
update: (_, moka, wood, essR) {
essR..mokaResourceModel=moka;
essR..woodResource=wood;
},
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)
],
child: MokaOnlineApp(),
);
}
}
class MokaOnlineApp extends StatelessWidget {
// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'MokaOnline',
home: Test(),
);
}
}
class Test extends StatelessWidget {
@override
Widget build(BuildContext context) {
return Consumer<GameController>(
builder: (context, gameControler, _) {
final _moka = gameControler.essentialResourceController.mokaResourceModel;
final _wood = gameControler.essentialResourceController.woodResource;
return new SafeArea(
child: Container(
child: Column(
children: <Widget>[
Text('Moka : ${_moka.getResource()}'),
FlatButton(
onPressed: () {
_moka.increment();
},
child: Text('Add Moka'),
),
new Container(height: 100.0,),
Text('Wood : ${_wood.getResource()}'),
FlatButton(
onPressed: () {
_wood.increment();
},
child: Text('Add Wood'),
)
],
),
),
);
},
);
}
}
class GameController extends ChangeNotifier {
GameController() {_EssentialResourceController = EssentialResourceController();}
EssentialResourceController _EssentialResourceController = EssentialResourceController();
EssentialResourceController get essentialResourceController => _EssentialResourceController;
set essentialResourceController(EssentialResourceController value)
{
_EssentialResourceController = value;
notifyListeners();
}
}
class EssentialResourceController extends ChangeNotifier {
EssentialResourceController() {
_MokaResourceModel = MokaResourceModel();
_WoodResource = WoodResource();
}
MokaResourceModel _MokaResourceModel = MokaResourceModel();
MokaResourceModel get mokaResourceModel => _MokaResourceModel;
set mokaResourceModel(MokaResourceModel value)
{
_MokaResourceModel = value;
notifyListeners();
}
WoodResource _WoodResource = WoodResource();
WoodResource get woodResource => _WoodResource;
set woodResource(WoodResource value)
{
_WoodResource = value;
notifyListeners();
}
}
class MokaResourceModel extends ChangeNotifier {
int _moka = 0;
void increment() {
_moka++;
notifyListeners();
}
@override
int getResource() {
return _moka;
}
}
class WoodResource extends ChangeNotifier {
int _wood = 0;
void increment() {
_wood++;
notifyListeners();
}
@override
int getResource() {
return _wood;
}
}
这样也好不了
ChangeNotifierProvider(create: (context) => MokaResourceModel()),
ChangeNotifierProvider(create: (context) => WoodResource()),
ChangeNotifierProxyProvider<MokaResourceModel, EssentialResourceController>(
key: UniqueKey(),
create: (_) => EssentialResourceController(),
update: (_, moka, essR) => essR..mokaResourceModel=moka
),
ChangeNotifierProxyProvider<WoodResource, EssentialResourceController>(
key: UniqueKey(),
create: (_) => EssentialResourceController(),
update: (_, wood, essR) => essR..woodResource=wood
),
ChangeNotifierProxyProvider<EssentialResourceController, GameController>(
create: (_) => GameController(),
update: (_, essR, gamec) => gamec..essentialResourceController=essR
)