0

I have problem with my code. I am trying to update child by set a function of setState to the parent then fired it from stateless widget once button clicked. It gonna looks like

Main (Stateful) -> Parent (Stateless) [There we fire the setState when TextField input changed] -> Child (Stateful)

Here is the code

Function stateGoldTotalBox = (){}; //storing the setState function here
class BuildGoldTotalBox extends StatefulWidget {
  final String type;
  BuildGoldTotalBox({required this.type});

  @override
  State<BuildGoldTotalBox> createState() => _BuildGoldTotalBoxState();
}

class _BuildGoldTotalBoxState extends State<BuildGoldTotalBox> {
  int _amount = 0;
  Map language = globals.language;
  Future<bool> _updateState({required int amount}) async{
    if(this.mounted) {
      setState(() {
        _amount = amount;
        print('fired'); //this on is working fine
      });
    }
    return true;
  }

  @override
  void initState() {
    // TODO: implement initState
    super.initState();
    stateGoldTotalBox = _updateState;
  }

  @override
  Widget build(BuildContext context) {
    print(_amount); //this also working to check if setState working
    return Container(
      decoration: BoxDecoration(
        border: Border.all(color: Colors.grey,width: 0.5),
      ),
      child: Padding(
        padding: const EdgeInsets.all(8.0),
        child: Row(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(language['total'].toString().toCapitalized()),
            Text(_amount.toStringAsFixed(2)), //THIS ONE DOES NOT CHANGE
            SvgPicture.asset('lib/assets/currency/money.svg',height: 20.0,),
          ],
        ),
      ),
    );
  }
}
6
  • 1
    First of all, it is not a good practice to modify the state of a widget via a static method. Also, all the code would be helpful, including the code where you actually call the method at the end.
    – Mäddin
    Apr 19, 2022 at 1:06
  • I saw an answer that use setState into parent then call it. Actually, I prefer use callback. However, there are many stateless widget that I have to pass the callback just to call the setState function. That is why, I put into parent and call it directly to update it. Anyway, thank you to let me know. I am gonna use Get package rather than doing that one. Anyway, what do you mean about calling method at the end. Do you mean about WidgetsBinding? Apr 19, 2022 at 6:46
  • 1
    You never have to pass to parents or children your setState method. I did the same in the first weeks with Flutter, before I learned better state management. The best way to do this is to use packages like Bloc, Redux or GetX. I wouldn't recommend GetX in general, because it makes small things easier, but more complex things often more difficult. But that is my opinion. For more options you should have a look here: docs.flutter.dev/development/data-and-backend/state-mgmt/…
    – Mäddin
    Apr 19, 2022 at 17:29
  • 1
    For complex projects I use Bloc (pub.dev/packages/flutter_bloc) and in places also self-written streams. But there are other good packages that I haven't tried yet. GetX is very fast to learn, but that doesn't make it the best for large projects. There are reasons why GetX is the most popular package on pub.dev, but nevertheless not a Flutter Favorite.
    – Mäddin
    Apr 19, 2022 at 17:50
  • 1
    Thank you, I am trying GetX atm since I have already implemented to the project. Next project I will try Bloc then compare which one the best. Thank you for your recommendation. I really appreciate it. Apr 19, 2022 at 17:54

1 Answer 1

1

try use get plugin, https://pub.dev/packages/get you can update stateless widget with it

1
  • Updating widgets manually is only practical for small projects. For larger projects I would recommend other state management packages.
    – Mäddin
    Apr 19, 2022 at 17:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.