How to notifyListeners with nested child from Provider in Flutter?

回眸只為那壹抹淺笑 提交于 2021-02-08 10:21:29

问题


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!


回答1:


you are looking for something like nested objects in a provider

like Flutter Provider Nested Objects

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;
  }
}



回答2:


something is wrong, but maybe you find the error

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;
  }

}



回答3:


this is also not really better

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
        )


来源:https://stackoverflow.com/questions/61046213/how-to-notifylisteners-with-nested-child-from-provider-in-flutter

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!