Flutter Getx: Can obs create a global variable? - flutter

How to make obs a global variable?
Have an option of Getx that can create a global variable?
For the example:
class Test extends GetxController {
RxString a = "".obs;
}
Page 1:
Test test = Get.put(Test());
print(test.a.value); //""
test.a.value = "55555";
print(test.a.value); //"55555"
Page 2:
Test test = Get.put(Test());
print(test.a.value); //""

You can insert your class into main with Get.put(Test()); and it would be something like:
void main() {
Get.put(Test());
runApp(...);
}
In your test class add the following code: static Test get to => Get.find<Test>();
Your Test class would look like this:
class Test extends GetxController {
static Test get to => Get.find<Test>();
RxString a = "".obs;
}
Now you have your global class and to send it to call it would be as follows:
Test.to.a
You would use it in the following way:
//IMPORT TEST CLASS
//get the same instance that has already been created
Test test = Test.to.a;
print(test.value);

Update
Now, I am using
Class + static
Class:
Class Global {
static Rx<String> name = "me".obs;
static ProfileObject profile = ProfileObject();
}
Use:
Obx(()=>Text(Global.name.value)),
Obx(()=>Text(Global.profile.age.value)),
It is easy to use, don't setting just use static in class.

Get.put() creates single instances, however, if you want to create a global controller, there's Get.find() which fetches the last instance of the controller created and shares it, that way global access can be achieved.
You can read more about this here
Note, to use Get.find(), an instance needs to be created earlier.
Your new code will look like this:
class Test extends GetxController {
RxString a = "".obs;
}
Page 1:
Test test = Get.put(Test());
print(test.a.value); //""
test.a("55555");
print(test.a.value); //"55555"
Page 2:
Test test = Get.find<Test>();
print(test.a.value); //"55555"

If your work needs the latest value you can follow the comment of #Sagnik Biswas. It works!
Now, I am using basic globals instances.
test_cont.dart:
TestCont testContGlobals = Get.put(TestCont());
class TestCont extends GetxController {
RxString a = "".obs;
}
But I still think my method might not be correct.
Are there any disadvantages to this method?

Related

How to communicate between two stateProviders in river pod?

i have just recently stated working with riverpod state mangement in flutter.
i have issue related to comunicate between to state providers.
here is my sample code:
class SomeClass_ONE extends stateNotifer <SomeState> {
SomeClass_ONE({required this.somevalue}):super(null);
final SomeCustomClass somevalue;
void methodOne(SomeState newstatevalue){
state = newstatevalue;
}
}
final someClassOneProvider =
StateNotifierProvider<SomeClass_ONE,SomeState>.
((ref)=>SomeClass_ONE(somevalue: SomeCustomClass()));
now i have another state provider class as below
class SomeClass_Two extends stateNotifer <SomeStateTwo> {
SomeClass_ONE({required this.somevalue}):super(null);
final SomeCustomClass somevalue;
void methodtwo(SomeState newstatevalue){
state = newstatevalue;
}
}
final someClassTwoProvider =
StateNotifierProvider<SomeClass_Two,SomeStateTwo>
((ref)=>someClassTwoProvider(somevalue: SomeCustomClass()));
now what i want to achhive is that on methodOne execution i have to listen that state cahnge and have to trigger methodTow and have to upate secondproviders state as well.
so how can i achive this without using Ref in class cunstroctors?
i have tried with ref.listner to trigger and have passed Ref in both class constructors. but as per some condition i can't use Ref directly in constructors as per some guideline followed by seniors.
You can pass a Ref ref object to the methodtwo method and then call the necessary methods from other StateNotifierProvider. In any case, to refer to other methods of other classes, you need to have a Ref object.
Try to use watch provided by StateNotifierProvider
Try this code:
class SomeClass_ONE extends stateNotifer <SomeState> {
SomeClass_ONE({required this.somevalue}):super(null);
final SomeCustomClass somevalue;
void methodOne(SomeState newstatevalue){
state = newstatevalue;
// Listen to the changes in the state of the first provider and call the methodtwo of the second provider
someClassTwoProvider.watch((_) => _.methodtwo(newstatevalue));
}
}

CommonJS: require class and extend class

I'm a bit struggling with the JavaScript classes (CommonJS).
I've got a class in a javascript 'module' which I can import to another js file using:
DucoMaster.js:
class DucoMaster {
constructor(node){
this.node = node;
}
}
module.exports = {DucoMaster}
DucoModules.js:
const {DucoMaster} = require("./DucoMaster");
...
let test = new DucoMaster;
console.log(test);
It builds and printing the test works, it prints the object as defined as class in DucoMaster.
Now I would like to import the module and use it to extend another class like:
'class DucoMaster extends ParentClass' within DucoModules.js
Is this possible with DucoModules.js?
Best regards,

Basics of Dart: Access class paramters

I have started learning Dart and Flutter and wanted to understand one concept:
Updated code: try in dartpad
class Service{
String ask = '';
void write (String receivedData){
ask = receivedData;
}
}
class WriteNow{
String hi = 'hi';
Service art = Service();
void okay () {
art.write(hi);
}
}
void main () {
WriteNow a = WriteNow();
a.okay();
Service b = Service();
print(b.ask);
}
I run WriteToService first and then ReadFromService, I cannot get the 'Hello', but get the original string ''. Please clarify. Also, how does this scale?
You are creating different instances of the Service class, that's the reason you can't get the updated String. Let me explain, in this piece of code:
WriteNow a = WriteNow();
a.okay();
You are creating an instance of the Service class, called art. The art instance has its member called ask, which is empty. When you call a.okay(), you are modifying the ask member of the art instance. So now, if you run this print(a.art.ask) you will get the 'hi' response.
Instead of that, you are creating another instance of the Service class, called b, and then you are printing the b.ask value. Can you see the error? You modified the art instance, not the b instance.
The ask value is not "global" to all the instances of the Service class, each instance has its own ask value, and each instance can change it without modifying the other instances.
class Service {
String ask = '';
void write (String receivedData){
ask = receivedData;
}
}
class WriteToService{
Service a = Service();
a.write('hello');
}
class ReadFromService {
Service b = Service();
print(b.ask);
}
What you are Doing:
Step 1: The Service class contains String ask = '';
Step 2: Running WriteToService class
Step 3: Running ReadFromService class
So ReadFromService class displays original String content which is ask = '' as it has been reassigned to String ask = ''. Scope of the previous entry hi of WriteToService class was ended.
Correct way to do it:
void main() {
WriteToService ok1 = new WriteToService();
ReadFromService ok2 = new ReadFromService();
}
String globelVariable = 'hi';
class Service {
String ask = globelVariable;
void write(String receivedData) {
globelVariable = receivedData;
}
}
class WriteToService {
Service a = new Service();
WriteToService() {
String name = "hello";
a.write(name);
}
}
class ReadFromService {
Service b = new Service();
ReadFromService() {
print(b.ask1);
}
}
Now declare a global variable String globelVariable = 'hi'; and assign it to String ask = globelVariable; in class Service. In write method assign the receiving data to global variable globelVariable = receivedData;. Since the scope of global variable doesn't end till the program is terminated it won't loose the value of the string hello which is eventually printed when ReadFromService class is called.
Test the above code at https://dartpad.dev/

How can I access global data from another class in Flutter?

I have a simple class like;
import 'dart:io';
class IDCardClass {
File frontImageFile;
File backImageFile;
}
From front_test.dart class I need to assign a data to frontImageFile and back_test.dart class I need to assign a data to backImageFile.
In my home.dart or another ***.dart class I need to get frontImageFile and backImageFile to show it to user.
My question is How can I access global data from another class in Flutter?
Make the variables to static like this:
class IDCardClass {
static File frontImageFile;
static File backImageFile;
}
Just import the class into the other class and access the variables.
import 'package:your_projectname/your_folder/IDCardClass.dart';
.....
var imageFile = IDCardClass.frontImageFile;
.....

How to access Number through separate class?

Hey everyone so this is something that I have always had trouble trying to accomplish or understand. So I have my main Engine class calledescapeEngine where I have a private var nScore I want to be able to access this variables through a separate class called mcPlanets but I don't know how I would accomplish this. I know how to do the opposite but not how to access a var from my main Engine class. Can anyone help me out?
I am not sure what you are trying to do, but here is an example that may help you:
Inside esacapeEngine class (main), create a public var nString and new instance of mcPlanets.
// two lines in escapeEngine.as
var nScore = 0;
var mcPlant = new mcPlanets(this);
So, when you create new mcPlanets, pass in the reference (keyword 'this' in the parentheses). Now mcPlanets knows about your main class.
And now in mcPlanets class, write this:
public class mcPlanets
{
private var escapeEngine;
public function mcPlanets(main) // 'this' = 'main'
{
escapeEngine = main;
// access nScore defined in main class
escapeEngine.nScore = 5;
}
}
In this example, nScore must be a public variable, it could be a private but you should use 'get and set' methods.