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

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

Related

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,

Flutter Getx: Can obs create a global variable?

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?

How to declare App-level variable for being used by all the components

So I have this App with a Firestore db variable, I'm currently passing the db through the widgets' constructor. Is there any way my widget can get the App's db variable without declaring a properties for it?
You can declare such a variable anywhere outside a class or function (top-level).
You make the shared variable available by importing the file that contains it.
Another way are static fields in classes.
You also need to import the library that contains the class and access it with the classname as prefix.
shared_var.dart
String globalFoo = 'foo';
class Global {
static bar = 'bar';
}
somewhere.dart
import 'package:my_package/shared_var.dart';
void foo() {
print(globalFoo);
}
void bar() {
print(Global.bar);
}
main.dart
import 'package:my_package/somewhere.dart';
void main() {
foo();
}

Ionic2: What is the best method to store static constants like api urls?

I would like to know the best method to store application wide static constants like API URLs and manage them in one place.
Well you can create enums and then import them into your files as follows:
create enums.ts and import it into your .ts file
export enum APIURL{
URL1 = "http://example.com",
URL2 = "http://example2.com"
}
and in your component.ts file
import * as Enums from './somedirectory/enums/enums';
...
constructor()
{
console.log(Enums.APIURL.URL1);
}

AS3 accessing stage from class

Timeline code:
import as3.first;
first._this=this;
var str1:String='this is timeline';
Class code:
package as3 {
import flash.display.MovieClip;
public class first extends MovieClip {
public static var _this:Object;
trace(_this.str1);
}
}
Error message:
TypeError: Error #1009: Cannot access a property or method of a null
object reference.
Trying to wrap my mind around how classes work. Need to access timeline functions and variables from class. What am I doing wrong and how can I make this work?
All in all what you are doing is somewhat weird. May be, you just want a document class for your SWF root? You as well could add a class to any movieclip in your library: both ways grant you access to timeline.
package as3
{
import flash.display.MovieClip;
public class first extends MovieClip
{
public static var _this:Object;
trace(_this.str1); // you may place code here... but consider this:
// this area is STATIC, the code here
// executes only once when class gets initialized,
// so, this happens BEFORE you assign first._this=this;
}
}