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

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');

Related

Flutter named parameters attribute value

I have a question i flutter
in some named parameters like color :
Or physics : in listviewbuilder the value is Colors.some color name in color and
Neverscroll in physics
My question is how to know that the attribute take this value although I tried to search in source code in flutter but i couldn't find out how
Thanks a lot
Theses are known as named constructor. Check this class
class MyClass {
final int a;
final String b;
final double c;
MyClass({required this.a, required this.b, this.c = 1.0});
}
Once you like to use this class you need to provide data like
MyClass(a: 1, b: "");
Note here a and b are required but c is using default value. Also, you can make nullable variables.
A good start guide will be language-tour

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");
}

Flutter Getx: initial value of obs variable set to null

Upon now I always use getx observable declarations like this:
var someString = ''.obs;
var someNumber = 0.obs;
and so on...
But what if some variables don't have an initial value at first, and I actually want them to be null and later change them?
For non null-safe (pre Dart 2.12), you can declare your observable variables like this:
final someVariable = Rx<Type>();
For example:
final someString = Rx<String>();
final someNumber = Rx<int>();
And for null-safety (Dart 2.12 or later), Just use Rxn<Type> instead of Rx<Type>.
For example:
final someString = Rxn<String>();
final someNumber = Rxn<int>();
If you don't have an initial value for your [Rx] value (at the first time), you need to use
final Rx<YourObject?> yourObject = (null as YourObject?).obs;
Or for more organizing your code, you can create a separate class, like this
class RxNullable<T> {
Rx<T> setNull() => (null as T).obs;
}
and use:
final Rx<YourObject?> yourObject = RxNullable<YourObject?>().setNull()
If anyone else does face this problem.
final Rx<YourObject?> yourObject = (null as YourObject?).obs;
will work.
but if there's any message that says "Unnecessary cast. Try removing the cast.", just add this comment
// ignore: unnecessary_cast
above the line, and then save.
You can declare your observable variables like this for (dart 2.18 and above):
Rx varName = (T()).obs;
Getx in flutter,
I search lot of time How class as observable
so fixed,
Like this ,
Rx<HomeModel?> mainModel =HomeModel(content: []).obs;

Is it possible to listen for changes, and upon a change, get those new values from a separate class and update variable in current class?

So, I am in my home_screen dart file and I have a Text in it where I want it to display the live value from another class's properties value. When I update the class's properties value from another script I want my home_screen Text to update to that new value. My class in a separate file is the following (simplified) :
class MyClass {
static int myNumber = 10;
}
I want the following Text to update with the value of myNumber in another file:
Text('iWantThisToDisplayLiveMyNumber'),
When I change the value of myNumber from yet another random script how do I get the Text to update in live time to always be myNumber?
Look into Providers.
class MyClass with ChangeNotifier {
static int myNumber = 10;
void changeNumber(){
myNumber = 1;
notifyListeners();
}
}
In your Widget class you can set up a "listener" like this:
int updatedNumber = Provider.of<MyClass>(context, listen: true).myNumber;
This is a basic and bad way of doing this. Please check out the "ChangeNotifier" "ChangeNotifierProvider" and "Provider.of" Sections in the docs here.
https://flutter.dev/docs/development/data-and-backend/state-mgmt/simple
Its pretty easy to get started with. Good luck
You can make use of the getter and setter functions.
In the example below we are printing happy birthday when the person's age changes:
class Person {
int _age;
Person(this._age);
int get age => _age;
set age(int newValue) {
print("Happy birthday! You have a new Age.");
//do some awsome things here when the age changes
_age = newValue;
}
}
The usage is exactly as you are used to:
final newPerson = Person(20);
print(newPerson.age); //20
newPerson.age = 21; //prints happy birthday
print(newPerson.age); //21

Flutter - Add decimals on a dynamic value field

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