How can I convert english numbers into another language numbers (I am using Bengali language) in Flutter? - flutter

I am getting english numbers (e.g. 123) as integer, now I want to show it in another language form (e.g. ১২৩).
I used a function and it also worked. But, it doesn't work in same stateful class more than once.
It worked for the first Text widget but not for the second one
Column(
children<Widget>:[
Text(
"${convertNumber(123)}"
),
Text(
"${convertNumber(5630)}"
)
]
)
String convertNumber(int eng){
String bengali = '';
for(int i = 0; i < eng.toString().length; i ++){
setState(() {
switch(eng.toString()[i]){
case '1':
bengali = bengali + '১';
break;
case '2':
bengali = bengali + '২';
break;
case '3':
bengali = bengali + '৩';
break;
case '4':
bengali = bengali + '৪';
break;
case '5':
bengali = bengali + '৫';
break;
case '6':
bengali = bengali + '৬';
break;
case '7':
bengali = bengali + '৭';
break;
case '8':
bengali = bengali + '৮';
break;
case '9':
bengali = bengali + '৯';
break;
default:
bengali = bengali + '0';
}
});
}
return bengali;
}
While I am using it for other numbers in the same stateful class, it doesn't work. And error was :
setState() or markNeedsBuild() called during build
This LowerHalf widget cannot be marked as needing to build because the framework is already in the process of building widgets. A widget can be marked as needing to be built during the build phase only if one of its ancestors is currently building. This exception is allowed because the framework builds parent widgets before children, which means a dirty descendant will always be built. Otherwise, the framework might not visit this widget during this build phase. The widget on which setState() or markNeedsBuild() was called was: LowerHalf

Do not call setState inside your convert function, just use your function to get a value.
Widgets rebuild after you call setState function. In your case, build calls setState that triggers build again.

Create new function and return string after converting it.
Below function is for converting number from English to Hindi or Gujarati based on users selected language for application.
translateMobileNumber(String mobileNumber) {
var mobileNumberTrim = mobileNumber.toString().trim();
var translatedNumber = '';
var currentLan = 'gu';
switch (Get.locale.toString().substring(0, 2)) {
case 'gu':
currentLan = 'gu';
break;
case 'hi':
currentLan = 'hi';
break;
case 'en':
currentLan = 'en';
break;
default:
currentLan = 'en';
break;
}
if (currentLan == 'en') {
return mobileNumberTrim;
}
for (int i = 0; i < mobileNumberTrim.length; i++) {
switch (mobileNumberTrim[i]) {
case '0':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૦' : '$translatedNumber૦';
break;
case '1':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૧' : '$translatedNumber१';
break;
case '2':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૨' : '$translatedNumber२';
break;
case '3':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૩' : '$translatedNumber३';
break;
case '4':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૪' : '$translatedNumber४';
break;
case '5':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૫' : '$translatedNumber५';
break;
case '6':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૬' : '$translatedNumber६';
break;
case '7':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૭' : '$translatedNumber७';
break;
case '8':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૮' : '$translatedNumber८';
break;
case '9':
translatedNumber =
currentLan == 'gu' ? '$translatedNumber૯' : '$translatedNumber९';
break;
}
}
return translatedNumber;
}

Related

Error handling in dart - Evaluating user input

I am try to evaluate the data type entered by the user so it won't break down user enters a string. so that I can tell the user to input a number. I have tried several ways but I don't I understand the "try-catch" algorithm well.
import "dart:io";
String prompt(String promptText) {
print("${promptText}");
String answer = stdin.readLineSync();
return answer;
}
double promptDouble() {
print("Enter your numbers");
double myNum = double.parse(stdin.readLineSync());
return myNum;
}
void main() {
String op = prompt("Enter an operation \n +\n -\n *\n /");
double num1 = promptDouble();
double num2 = promptDouble();
switch (op) {
case '+':
print("The sum of ${num1} and ${num2} is ${num1 + num2} ");
break;
case '-':
print("The difference of ${num1} and ${num2} is ${num1 - num2} ");
break;
case '*':
print("The product of ${num1} and ${num2} is ${num1 * num2} ");
break;
case '/':
print("The quotient of ${num1} and ${num2} is ${num1 / num2} ");
break;
default:
print("Sucker! Invalid Operator.");
}
}
I removed dart:io and replaced it for a static input because i'm using dartpad.dev.
Wrap the lines you know that can give you an error in the try block, next you can handle the error in the catch block, where you have access to the error. In my case I've printed the error and set the myNum to 0
double promptDouble(String str) {
print("Enter your numbers");
double myNum;
try {
myNum = double.parse(str);
} catch (e) {
print(e);
myNum = 0;
}
return myNum;
}
void main() {
String op = '+';
double num1 = promptDouble('10');
double num2 = promptDouble('hello');
switch (op) {
case '+':
print("The sum of ${num1} and ${num2} is ${num1 + num2} ");
break;
case '-':
print("The difference of ${num1} and ${num2} is ${num1 - num2} ");
break;
case '*':
print("The product of ${num1} and ${num2} is ${num1 * num2} ");
break;
case '/':
print("The quotient of ${num1} and ${num2} is ${num1 / num2} ");
break;
default:
print("Sucker! Invalid Operator.");
}
}
in your case yo could simply change the promptDouble function:
double promptDouble() {
print("Enter your numbers");
double myNum;
try {
myNum = double.parse(stdin.readLineSync());
} catch (e) {
print(e);
myNum = 0;
}
return myNum;
}

How can you setup a flutter rawkeyboardlistener to read {} (curly brackets) instead of square brackets?

I have a flutter solution that interprets a QR code via a scanning device. I use the RawKeyboardListener to read the scanned QR code that then parses it into a variable.
The QR value is an object {something: somethingelse}, but when read by the RawKeyboardListener, it reads the {} as [] and as a result, the object is then invalid.
String runtime = event.runtimeType.toString();
if (event.data.logicalKey != LogicalKeyboardKey.enter) {
setState(() {
scannedItem += runtime == 'RawKeyUpEvent' ? event.data.keyLabel : '';
});
} else if (scannedItem != '') {
setState(() {
var data = json.decode(scannedItem);
});
}
I played around with the solution and ended up fixing it, though I do feel there is a better way of resolving this
if (event.data.isShiftPressed) {
switch (event.data.logicalKey.debugName) {
case 'Bracket Left':
keyLabel = '{';
break;
case 'Bracket Right':
keyLabel = '}';
break;
case 'Quote':
keyLabel = '"';
break;
case 'Semicolon':
keyLabel = ':';
break;
default:
keyLabel = keyLabel.toUpperCase();
break;
}
}

The name of the variables are repetitive and there must be some way to reduce the code

can this be reduced?
I think there must be some way.
One way or another.
switch (i) {
case 1:
clicked1 = true;
break;
case 2:
clicked2 = true;
break;
case 3:
clicked3 = true;
break;
case 4:
clicked4 = true;
break;
}
Consider making a list:
List<bool> clicked = [false, false, false, false] // Fixed list of size 4
// use new List() if unknown size
And in your code:
void setTrue(int i){
// Since i is from 1-4, and array index is 0-3, i-1 is needed
clicked[i-1] = true;
}
You can also toggle instead of just setting true:
void toggle(int i){
clicked[i-1] = !clicked[i-1];
}
How about just checking them one by one?
bool clicked1 = i == 1;
bool clicked2 = i == 2;
bool clicked3 = i == 3;
bool clicked4 = i == 4;

Cancel Or Exit from PromptDialog.Choice Flow in Microsoft Bot Framework

i have a Prompt with 4 options, the last option is user can quit the Prompt,
i want to implement some code so that the bot exit the Prompt
Image
PromptDialog.Choice(context, this.OnOptionSelected, new List<string>() { FlightsOption, HotelsOption, TrainOption, GobackOption }, "Sure..! Tell me what booking would like to make..?", "Not a valid option", 3);
in above image i had implemented quit option on which if user selects quit it goes to Switch case of quit.
i had also tried context.quit but it throws error
private async Task OnOptionSelected(IDialogContext context, IAwaitable<string> result)
{
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
//want some code here to quit the form
break;
}
}
First of all this is not a Form Flow. This is prompt.
Now You can do something like, either you exit the dialog from the stack like this
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
context.Done<object>(null);
break;
}
}
Or,
You can tell something and then wait for other message in the same dialog like this
try
{
string optionSelected = await result;
switch (optionSelected)
{
case FlightsOption:
context.Call(new FlightDialog(), this.ResumeAfterOptionDialog);
break;
case HotelsOption:
context.Call(new HotelsDialog(), this.ResumeAfterOptionDialog);
break;
case TrainOption:
context.Call(new TrainDialog(), this.ResumeAfterOptionDialog);
break;
case GobackOption:
await context.PostAsync("Ok, you came back. Now tell something new.");
context.Wait(MessageReceivedAsync);
break;
}
}
And the next message will go here
public virtual async Task MessageReceivedAsync(IDialogContext context, IAwaitable<IMessageActivity> result)
{
var message = await result;
context.Wait(MessageReceivedAsync);
}

Replace number with name of months

I have sort of data which contains number.
1,2,3,4,5,6,7,8,9,10,11,12, to represent months.
I want to change it so when I preview the report it shows
January
February
March
April
May
June
July
August
November
December
I've tried using replace()
.replace("1", "January")
When I run it, it doesn't work
Any suggestion?
You can use Text Field Expression, Replace $V{Month} with your field Name
<textfieldexpression class="java.lang.String">
(
$V{Month}.equals("1") ? "January" :
$V{Month}.equals("2") ? "February" :
$V{Month}.equals("3") ? "March" :
$V{Month}.equals("4") ? "April" :
$V{Month}.equals("5") ? "May" :
$V{Month}.equals("6") ? "June" :
$V{Month}.equals("7") ? "July" :
$V{Month}.equals("8") ? "August" :
$V{Month}.equals("9") ? "September" :
$V{Month}.equals("10") ? "October" :
$V{Month}.equals("11") ? "November" : "December"
)
</textfieldexpression>
This should solve your problem. What I have done here is split the source string into an ArrayList of String and then used switch case to concatenate the names of corresponding months to a new string that is shown as the output.
import java.util.*;
class abc
{
public static void main (String[] args)
{
String str = "1,2,3,4,5,6,7,8,9,10,11,12,";
String res = "";
List<String> list = new ArrayList<String>(Arrays.asList(str.split(",")));
for (String s : list)
{
switch (s)
{
case "1":
res += "January\n";
break;
case "2":
res += "February\n";
break;
case "3":
res += "March\n";
break;
case "4":
res += "April\n";
break;
case "5":
res += "May\n";
break;
case "6":
res += "June\n";
break;
case "7":
res += "July\n";
break;
case "8":
res += "August\n";
break;
case "9":
res += "September\n";
break;
case "10":
res += "October\n";
break;
case "11":
res += "November\n";
break;
case "12":
res += "December\n";
break;
default:
System.err.println("Invalid output");
}
}
System.out.println(res);
}
}