Getx: Please make listenable True Function var pro = Provider.of<ButtonModel>(context, listen: true);

Created on 15 Oct 2020  路  11Comments  路  Source: jonataslaw/getx

Please make listenable True Function var pro = Provider.of(context, listen: true);

invalid

All 11 comments

This is not the correct way to request a feature

Sorry for this,
Please make listenable True Function in GetX.
Controller controller = Get.put(Controller()); is not for update.
please make it for update or rebuild widgets.

I didn't understand what you're talking about.
Could you show a valid source code to demonstrate the current behavior and explain the behavior you want?

This Code is not Working-
96107572-33e21700-0efa-11eb-9952-3262f3a88142

But This Code is working-
96107313-e82f6d80-0ef9-11eb-9613-e1e55222b9bc

If your goal with this is just to change the theme of the application, I suggest you see this code:
https://gist.github.com/RodBr/37310335c6639f486bb3c8a628052405
Reference: https://medium.com/swlh/flutter-dynamic-themes-in-3-lines-c3b375f292e3

But if you want to have access to the controller from anywhere, Get.put must be above runApp and with the permanent property equal to true:

void main() {
  Get.put(Themex(), permanent: true);
  runApp(App());
}

and in your widget put Get.find ():

final Themex themex = Get.find<Themex>();

Anyway, it will be necessary to use something to observe the changes, in this case Obx. See the code working:

import 'package:flutter/material.dart';
import 'package:get/get.dart';

void main() {
  Get.put(Themex(), permanent: true);
  runApp(App());
}

class App extends StatelessWidget {

  final Themex themex = Get.find<Themex>();

  @override
  Widget build(BuildContext context) {
    return Obx(() => MaterialApp(
      home: Scaffold(
        backgroundColor: themex.bgColor.value,
        body: Center(child: RaisedButton(onPressed: () => themex.changeTheme())),
      ),
    ));
  }

}

class Themex extends GetxController {
  Rx<Color> bgColor = Colors.white.obs;
  RxBool darkTheme = false.obs;

  void changeTheme() {
    darkTheme.value = !darkTheme.value;
    bgColor.value = darkTheme.value ? Colors.black : Colors.white; 
    print('>>> ${darkTheme.value}');
  }
}

Okay, I need to use Obx for observe changes Directly Get.put not working, I understood.
I have a Last question, is Obx Rebuild total StatelessWidget ?

See explanation here:
https://github.com/jonataslaw/getx/blob/master/documentation/en_US/state_management.md#getbuilder-vs-getx-vs-obx-vs-mixinbuilder
With Obx only what is changed and if it is really changed will be rebuilt

Why you're using permanent: true ?
I don't find it in docs for that reason i asking you.

As you were comparing to the provider, I believe that you would need this control on other routes, so permanent configures for the controller never to be removed from memory
See explanation here:
https://github.com/jonataslaw/getx/blob/master/documentation/en_US/dependency_management.md#getput

This is not a concise feature request, and it doesn't seem to have anything to do with this package, so I'm closing it

Thank You for answering me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

R-Praveen picture R-Praveen  路  4Comments

Denilson-source picture Denilson-source  路  3Comments

GoldenSoju picture GoldenSoju  路  3Comments

DarkHeros09 picture DarkHeros09  路  3Comments

manojeeva picture manojeeva  路  3Comments