essentialResourceController

essentialResourceController

main.dart: import'package:flutter/material.dart';导入'package:provider/provider.dart';导入'controllers/GameController.dart';void main()=>runApp(MokaOnline());MokaOnline类扩展StatelessWidget {@override窗口小部件build(BuildContext context){返回MultiProvider(提供者:[ChangeNotifierProvider(create:(context)=> GameController()),],子代:MokaOnlineApp(),);}}MokaOnlineApp类扩展了StatelessWidget {//此小部件是您的应用程序的根.@override窗口小部件build(BuildContext context){返回MaterialApp(标题:"MokaOnline",主页:Test(),);}}类Test扩展了StatelessWidget {@override窗口小部件build(BuildContext context){返回Consumer< GameController>(生成器:(上下文,gameControler,_){final _mokaResource =gameControler.essentialResourceController.mokaResource;返回容器(子:列(子代:< Widget> [Text('Moka:$ {_ mokaResource.getResource()}'),FlatButton(onPressed:(){_mokaResource.increment();},子级:Text('Add Moka'),)],),);},);}} GameController.dart: import'package:flutter/foundation.dart';导入'package:mokaonline/controllers/EssentialResourceController.dart';类GameController {EssentialResourceController _essentialResourceController =EssentialResourceController();EssentialResourceController获取essentialResourceController =>_essentialResourceController;} EssentialResourceController.dart: import'package:mokaonline/models/essentialResources/MokaResourceModel.dart';导入'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';class EssentialResourceController {ResourceEssentialInterface _mokaResource = MokaResourceModel();ResourceEssentialInterface get mokaResource =>_mokaResource;} MokaResourceModel.dart: import'package:flutter/foundation.dart';导入'package:mokaonline/models/essentialResources/ResourceEssentialInterface.dart';MokaResourceModel类扩展ChangeNotifier实现ResourceEssentialInterface {int _moka = 0;无效的增量(){_moka ++;notifyListeners();}@overrideint getResource(){返回_moka;}} 我知道UI不会刷新,因为它不是真正更新的"GameController" ...但是我不知道如何构造代码.无论如何,谢谢您的耐心,祝您有愉快的一天!解决方案您正在寻找提供者中的嵌套对象之类的东西像 Flutter Provider嵌套对象 import'package:flutter/material.dart';导入'package:provider/provider.dart';导入'package:flutter/foundation.dart';void main()=>runApp(MyApp());MyApp类扩展了StatelessWidget {@override窗口小部件build(BuildContext context){返回MultiProvider(提供者:[ChangeNotifierProvider(create:(context)=> MokaResourceModel()),ChangeNotifierProxyProvider< MokaResourceModel,EssentialResourceController>(创建:(_)=>EssentialResourceController(),更新:(_,moka,essR)=>essR..mokaResourceModel = moka),ChangeNotifierProxyProvider< EssentialResourceController,GameController>(创建:(_)=>GameController(),更新:(_,essR,gamec)=>gamec..essentialResourceController = essR)],子代:MokaOnlineApp(),);}}MokaOnlineApp类扩展了StatelessWidget {//此小部件是您的应用程序的根.@override窗口小部件build(BuildContext context){返回MaterialApp(标题:"MokaOnline",主页:Test(),);}}类Test扩展了StatelessWidget {@override窗口小部件build(BuildContext context){返回Consumer< GameController>(生成器:(上下文,gameControler,_){final _mokaResource = gameControler.essentialResourceController.mokaResourceModel;返回新的SafeArea(子代:集装箱(子:列(子代:< Widget> [Text('Moka:$ {_ mokaResource.getResource()}'),FlatButton(onPressed:(){_mokaResource.increment();},子级:Text('Add Moka'),)],),),);},);}}GameController类扩展ChangeNotifier {GameController(){_EssentialResourceController = EssentialResourceController();}EssentialResourceController _EssentialResourceController = EssentialResourceController();EssentialResourceController获取essentialResourceController =>_EssentialResourceController;设置essentialResourceController(EssentialResourceController值){_EssentialResourceController =值;notifyListeners();}}EssentialResourceController类扩展ChangeNotifier {EssentialResourceController(){_MokaResourceModel = MokaResourceModel();}MokaResourceModel _MokaResourceModel = MokaResourceModel();MokaResourceModel get mokaResourceModel =>_MokaResourceModel;设置mokaResourceModel(MokaResourceModel值){_MokaResourceModel =值;notifyListeners();}}MokaResourceModel类扩展ChangeNotifier {int _moka = 0;无效的增量(){_moka ++;notifyListeners();}@overrideint getResource(){返回_moka;}} guys,I'm starting with flutter and I decided to make a mini game like "cookie clicker" to train but I've just run into a problem with the state management...I use a provider to propagate the "GameController" which aims to update the player's resources and do the calculations.I manage to get the "GameController" everywhere but when I interact with a resource (object in the "GameController") and this one is up to date but not in the UI, it doesn't change...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; }}I understood that the UI was not refresh because it's not really the "GameController" that is updated... But I don't see how to structure the code otherwise.Anyway, thank you for your patient and have a nice day! 解决方案 you are looking for something like nested objects in a providerlike Flutter Provider Nested Objectsimport '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; }} 这篇关于如何从Flutter中的Provider通知带有嵌套子项的侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
09-14 15:06