Flutter - Add decimals on a dynamic value field - flutter

In Flutter i have a dropdown (we can call maxim field) to select total length. Then in index field we can add values with the same length of dropdown. When i add the value in index field i need to add '.000'.
I try to use masked package but i can't concatenate the index field value.
The code is similar to
int inputMaxValue=1;
var inputIndexValue = new MaskedTextController(mask: inputMaxValue.text+'.000');
Return
Only static members can be accessed in initializers
¿Anybody know can i fix this problem?

I think MaskedTextController is unnecessary in your case, simply change int inputMaxValue to double inputMaxValue and possibly add .toStringAsFixed(3) to achieve 3 decimals after the dot.
e.g.
double inputMaxValue = 1.0;
String inputIndexValue = "${inputMaxValue.toStringAsFixed(3)}";
However, if you want to keep the flutter_masked_text package - you should avoid using dynamic strings for the mask property. Error Only static members can be accessed in initializers appears supposedly because you declare the masked controller variable within the class, not in it's enclosing methods (e.g. initState).
In case of MaskedTextController, only static strings should be used for the mask property.
e.g. this is what I use to format phone number field:
MaskedTextController(mask: "(000) 000-0000")

maybe NumberFormat can help
import 'package:intl/intl.dart';
final price = 123;
final formater = NumberFormat("#,##0.000");
print(formater.format(price));
output: 123.00

Related

Set Duration from parameters in Flutter [duplicate]

Why does this code:
class _SequentialTextPageState {
String jsonTextPref = 'seqtext';
int jsonTextSuff = 10;
String jsonText = jsonTextPref + jsonTextSuff.toString();
}
generate these errors?
Error: The instance member 'jsonTextPref' can't be accessed in an initializer.
Error: The instance member 'jsonTextSuff' can't be accessed in an initializer.
It seems to me that concatenation between String and int is correct?
Dart initializes objects in multiple phases. Initializing members directly ("field initializers") occurs early in object initialization, before this becomes valid, so that phase cannot initialize members that depend on other parts of the object.
Dart provides multiple ways to initialize members, so if one member needs to depend on another, you can initialize it in a later phase by using a different mechanism. For example, you could do one of:
Add the late keyword to make the dependent member lazily initialized.
Move initialization of the dependent member into the constructor body.
In the case of a Flutter State subtype, you could initialize the dependent member in its initState method, which in some cases is more appropriate.
Note that in some cases you additionally can consider replacing the member variable with a read-only getter instead. For example, in your case, perhaps you could use:
String get jsonText => jsonTextPref + jsonTextSuff.toString();
That would be appropriate if jsonText should always depend on jsonTextPref and jsonTextSuff, would never need to have an independent value, and if it's acceptable for jsonText to return a new object every time it's accessed.
Dart does not allow field initializers to refer to the object itself. Fields must always be fully initialized before any access is given to the object begin created.
The initializers can only access static and top-level variables, not any instance variables on the object itself.
With null safety, you will be allowed to write late String jsonText = this.something + this.other;. That field will then not be initialized until it's first read or written, which is necessarily after the object itself has been created.
You can only use constant expressions as initializers. x=this.y is not constant.
The error was displayed when I did following:
class MyClass {
String id;
String[] imagePaths;
}
It will mark the String in the line String id; as error, but the error is in the next line, it should be List<String> imagePaths; instead of String[] imagePaths; then the error in the line above also disappears. This can be very confusing if you have a big class and the actual error is many lines underneath the first marked line (talking from experience...)

How can I get variable value from a text string?

I have a constants file with text values, for example:
const String loginIntro1 = "Login to account";
const String loginButton = "Login";
And I also have a Map containing similar info, for example:
{"loginIntro1":"Login to account","loginButton":"Login"}
The idea is that the Map (derived from a JSON file) takes precedence, but if the value doesn't exist, the constants value is used instead.
In my text widget, I want to grab the text by a method call, such as:
getText('loginIntro1');
Is this even possible? How can I get the value from the constants file with a String, rather than a direct reference to the variable? Perhaps I'm missing a much simpler way of achieving this!

How to take integer input from user in dart

i am new in dart and just want to know how to take integer input from user in dart with null safety. i found out a way to take number input from dart which is:
String? chossenNumber = stdin. readLineSync();
if(chossenNumber !=null)
{
int number = int.parse(chossenNumber);
}
but i am unable to use number variable outside of the scope. Please tell me a way to solve this issue.
You can define the variable at the top of the class and initialize it here so you will be able to use it everywhere in the class
The solution of it very simple just take input of number as String i.e
String? chossenNumber = stdin. readLineSync();
and when you want to use this variable parse it to the 'int' i.e
if(int.parse(chossenNumber) <100)
{
print("Your Statement");
}

changing a string to a int in flutter by using tostring but flutter show an error

i am trying to make a textfield form for the user to input the some numbers in it. then I want to use the number and send it into Duration class. this Duration class can only accept int but my textfield form is text, I cannot change it into int. after some research it say can use to string but flutter tells me the instance member of hourController can't be accessed in an initializer. can any one help.
this is my code
final hourController =TextEditingController();
//this is the textformfield that I use I name it
//hourController. here the user will input numbers
//only.
int durationhour = hourController;
hourController = durationhour.toString();
this is your text editing controller
final hourController = TextEditingController();
this is how string convert to integer
int durationhour = int.parse(hourController.text);
and this is how you can convert integer to string
hourController.text = durationhour.toString();
You can use the int.parse(string) method:
var one = int.parse('your string');

How to fetch value from custom multifield component?

I have created a multifield custom widget having two fields with names ./urlLink and ./urlText.
Now i m trying to fetch the values from widget into the component's jsp with following code
String property = properties.get("./urlLink",String[].class);
for(String value: property ) {
out.print(value);
}
out.print(property);
But i am not able to get its value instead i m getting error.
If you're getting a property and it contains a string value, you need to use the method getString() - that way when you have the property, you can set the string to the value by doing something like this:
Property property = properties.get("./urlLink",String.class);
String value = property.getString();
Just a side note, if your return is supposed to be a string array, your type that you're putting the values in should be a string array.
String[] value
Check out the documentation on day.com for Properties and getting the values inside them.
Looks like a typo: you don't prefix a property name with .\ when accessing it.
My guess is you got a NullPointerException, right? That's because there's no ./urlLink property in the value map (properties). You should check against that anyway (so that it's not thrown on a fresh page with no content).
If that doesn't help -- double check that you have the properties in the content (call your page with .xml or .infinite.json extensions, and then double check if you can read them as plain strings (you should be able to -- CRX does some magic, smart type conversions).
It's good to register custom xtype as :
// registering the custom widget with the name dualfield
CQ.Ext.reg("dualfield", CQ.Ext.form.DualField);
Then u can easily fetch the value as :
String[] data = properties.get("multi",String[].class);
Here multi is the name of widget having multifield as xtype