i am using flutter and stackoverflow for the first time, forgive me errors.
I have global variables in a global.dart file.
In my main I want to access and edit a variable by passing it the value
Global.dart
String nome_impianto1 = "IMPIANTO 1";
String nome_impianto2 = "IMPIANTO 2";
String nome_impianto3 = "IMPIANTO 3";
String nome_impianto4 = "IMPIANTO 4";
main.dart
import 'package:services/global.dart' as globals;
globals.nome_impianto1 = usrcontroller_ovrelay_impianto.text; //so it works.
how can I pass the value I want to change?
globals.nome_impianto{$'1'} = usrcontroller_ovrelay_impianto.text //I know it doesn't work, and just to get the idea;
thank to all
Best regards
Andrea
Avoid using global variables in flutter. If you are managing state gloabally use StatefulWidget and even better start using provider or even more advanced bloc.
For your question, if you want to access these globally it is better to use a Map
// Global.dart
Map<String, String> globals = {
"nome_impianto1": "IMPIANTO 1",
"nome_impianto2": "IMPIANTO 2",
"nome_impianto3": "IMPIANTO 3",
"nome_impianto4": "IMPIANTO 4",
};
// main.dart
int index = 1;
globals["nome_impianto$index"] = usrcontroller_ovrelay_impianto.text;
flutter doesn't have reflection https://stackoverflow.com/a/49871692/8608146
Add this to any of your function:
setState((){
globals.nome_impianto1 = usrcontroller_ovrelay_impianto.text;
});
Related
I've been working with flutter-hooks' useFuture method, but I need to use it conditionally (based on user-input in this case). I've found this approach, but I need to know if there is a better way:
Storing what data is selected using useState, and if there is some data selected, change the future from null to the actual future. Example code:
final selected = useState<int>(null);
final future = useMemoizedFuture(() => selected.value != null ? http.get("someapi") : null);
Use the data as the keys of the memoizer. Then add the conditions for working with the future in the code where you need it, not in the hooks themselves.
final selectedIndex = useState<int?>(null);
final someFuture = useMemoizedFuture(() => http.get("someapi"), [useSelected.value]);
I have stored in shared_preferences key value pairs like below....
item_001 = 'some data'
item_103 = 'some data'
item_007 = 'some data'
item_059 = 'some data'
I am trying get all the stored values begins with item_***
I know how to read and write with single key (example below)... but I am trying to get a list of items from shared_preferences where the key name begins with item_.
string
read: final myString = prefs.getString('my_string_key') ?? '';
write: prefs.setString('my_string_key', 'hello');
stringList
read: final myStringList = prefs.getStringList('my_string_list_key') ?? [];
write: prefs.setStringList('my_string_list_key', ['horse', 'cow', 'sheep']);
due to some reason, I don't want to store all the items in one list.... I want to store each item with separate key.
I searched in google and in stackoverflow, unfortunately no where found proper answer....
also I looked into this one, but not understood how to implement partial key search...
esetintis got to this first but I doodled this code so I guess I'll share it. But yes, you have to first get all of the keys in the shared preferences and then get the value for matching keys.
SharedPreferences prefs = await SharedPreferences.getInstance();
Set<String> keys = prefs.getKeys().where((key)=>key.startsWith('item_'));
for (String key in keys) {
String value = prefs.getString(key); // Throws an error if you store something other than a String
// Do your thing
}
I don't think there is a way to make such query. However, you can tackle the issue with some extra steps.
1 step :
Get all keys from SharedPreferences with prefs.getKeys() method. This will return a Set of keys. Now you can assign a List<String> keys = prefs.getKeys().where((k)=>k.startsWith('item_')) which will have the keys you want to get from the Storage.
2nd :
Iterate the filtered array and get the values you want by calling SharedPreferences, and save them to some variable.
Assuming that you have all the keys in a list, for example:
List<String> keys = ['item_001', 'item_103', 'item_007', 'item_059', 'other_key', 'blablabla'];
Now, you could iterate through all of them and checking the ones that starts with "item_", like this:
var itemKeys = [];
for(var i=0;i<keys.length;i++){
if (keys[i].startsWith('item_')) {
itemKeys.add(keys[i]);
}
}
print(itemKeys); // [item_001, item_103, item_007, item_059]
In the above example, itemKeys contains all the needed keys for you. What you could also do is to add the proper logic to fetch values from the shared preferences inside the if statement in the loop:
var result = [];
for(var i=0;i<keys.length;i++){
if (keys[i].startsWith('item_')) {
result.add(prefs.getString(keys[i]) ?? '');
}
}
result should contain what are you looking for.
I have a bunch of Flex pages I need to convert to get rid of the flash player and have been unable to see how to replicate this code just using the javascript.
The Flex code gathers up data and sends it in a POST to a Cold Fusion page in another frame (named FrameData). Cold Fusion accesses the data from a FORM variable (FORM.mydata1, FORM.mydata2, etc.).
var RequestSite:URLRequest;
OutputPageValues.OutputType = 5;
OutputPageValues.mydata1 = "2";
OutputPageValues.mydata2 = "test";
RequestSite = new URLRequest("pageurl.cfm"));
RequestSite.data = OutputPageValues;
RequestSite.method = URLRequestMethod.POST;
navigateToURL(RequestSite, 'FrameData');
How do I emulate this construct in Royale? Is there another way to do this?
equivalente code for URLRequest:
var u:URLRequest = new URLRequest("http://domain.foo");
navigateToURL(u,"_blank");
in Apache Royale is BrowserWindow:
import org.apache.royale.core.BrowserWindow;
var u:String = "http://domain.foo";
BrowserWindow.open(u, "_blank");
To pass variables you need to do via GETmethod: "http://domain.foo?variable=" + key.
To use POST method use HTTPService class from Network SWC instead:
import org.apache.royale.net.HTTPConstants;
import org.apache.royale.net.HTTPService;
import org.apache.royale.net.URLVariables;
// add the variables to send
var urlVars:URLVariables = new URLVariables();
urlVars.set("variable", key);
// create the httpservice instance
var service:HTTPService = new HTTPService();
service.url = "http://domain.foo";
service.method = HTTPConstants.POST;
service.addEventListener("complete", resultCallback);
service.addEventListener("ioError", faultCallback);
// add the variables
service.contentData = urlVars;
// trigger the service
service.send();
Optionally in case you need to deal with CORS you can add CORSCredentialsBead bead to the HTTPService:
service.addBead(new CORSCredentialsBead(true));
(Note: code is untested, please report if all is ok so we can improve this response and code snippet, thanks)
I have two files named
abc.dart
String one = "One";
String two = "Two";
and xyz.dart
String one = "1";
String two = "2";
In my app, I have a bool value, if it is true I want to use one as "One" and if false, one should be "1".
That is I need to import abc.dart on true and xyz.dart on false.
According to this issue, it seems there is not possible in Flutter.
This is because a Flutter app is compiled AOT (ahead of time), so it needs to load all the sources at compile time.
So, instead of counting on dynamic modules imports, your options are:
using compile time constants
// letter.dart
class Letters {
final String one;
final String two;
const Letter(this.one, this.two);
}
// abc.dart
const ABC = Letters('one', two');
// xyz.dart
const XYZ = Letters('1', 2');
// main
import './abc.dart'
import './xyz.dart'
onPressedHandler() {
print(boolValue? abc.one : xyz.one);
}
specify the files as assets and parse them in run time.documentation about using assets
I want to send json data or a variable to Page2 from Page1. My codes as follows. I guess I writing wrong code. I don't know how to do this.
Page1:
var value1="test";
var jsonData=[];
jsonData.push("Value1");
jsonData.push("Value2");
jsonData.push("Value3");
Page2
var TextBox = new SMF.UI.EditBox({
width:"88%",
height:"100%",
left:"22%",
top:"0%",
text:"",
});
TextBox.text = Pages.Page1.value1;
TextBox.text = Pages.Page1.jsonData;
Pages.Page1.add(TextBox);
You could just add that variable to the Global file and then use it where you wish. Like:
//Global.js
var myVar;
//Page1.js
Label.text = myVar;
//or even change its value
myVar = "Hello";
But I am looking for a way of doing this without declaring a global variable too.