Returning an integer in dart - firebase

I want to do something like this:
int _radioValueGender == _radioValueGender
void setRadioValueGender(DocumentSnapshot document) {
if (document['gender']=='female') {
return _radioValueGender == 0;
}
else {
return _radioValueGender == 1;
}
}
Where I set the value of an integer _radioValueGender depending on the result of an if/else statement. I would be really appreciative of any help. I hope that this is easy, it just seems to be a question of knowing the right methods. I apologize for any inconvenience and thanks in advance.

== is the equality comparison operator. It does not do variable assignment.
setRadioValueGender is declared to have a void return type. It is incorrect to return values from it.
What you want is:
void setRadioValueGender(DocumentSnapshot document) {
if (document['gender']=='female') {
_radioValueGender = 0;
} else {
_radioValueGender = 1;
}
}
or more concisely:
void setRadioValueGender(DocumentSnapshot document) {
_radioValueGender = (document['gender'] == 'female') ? 0 : 1;
}

Simply modify your initial int _radioValueGender == _radioValueGender declaration and make it int _radioValueGender;. That way it forces the value to be any int object.
Then instead of result use setState(() {_radioValueGender =
0 }); in order to modify your variable anytime 'setRadioValueGener' has been pressed.

== is a comparison, when you type that you are asking if the two values on the left and the right are equal. = is an assignment, when you type it you are assigning the value on the left to the right. so just change it to this:
void setRadioValueGender(DocumentSnapshot document) {
if (document['gender']=='female') {
return _radioValueGender = 0;
}
else {
return _radioValueGender = 1;
}
}

Related

How to create a function in arduino?

I cannot get a function for this code to really work.
lastVal = val;
val = digitalRead(DT);
if (val == 1 && lastVal == 0)
{
if (digitalRead(CLK) == 1)
{
pos++;
}
else
{
pos--;
}
}
Can somebody pleas help me?
I am not sure if this is your entire code or not, but in case that is all of the code then I know the reason. Arduino requires the basic setup and loop functions to be referenced in the code, as long as it is referenced you should be fine - you can even leave the inside of the functions empty. You have not really asked the question very well so it is hard to see what you mean.
To create a function you can use this code:
void function_name_here(_parameters_here_)
{
//Code Here
}
To reference that function you just declare it by using:
function_name_here();
By the looks of it you might want to put your code in to the loop function, your code might look like this:
int DT = /* Value here */;
int pos = /* Value here */;
void setup()
{
pinMode(DT, INPUT);
}
void loop()
{
lastVal = val;
val = digitalRead(DT);
if (val == 1 && lastVal == 0)
{
if (digitalRead(CLK) == 1)
{
pos++;
}
else
{
pos--;
}
}
}

K-th largest element in BST

I am trying to find the K-th largest element in a Binary Search Tree using reverse inorder approach by using a counter. Here is what I have implemented:
int klargest(Node root,int k,int count)
{
if(root != null)
{
klargest(root.right,k,count);
count++;
if(count == k)
return root.data;
klargest(root.left,k,count);
}
return -1;
}
But the issue is that when count = k, the code does not return the answer to the caller function but instead to a sub-call. Due to this, the answer is lost. In other words, the recursion does not stop there and it keeps on going until all the nodes are visited. In the end, I get the answer -1. What I want is that the recursion should end when count = k and the required answer should be returned to the caller function. How can I do this?
Note: I neither want to use a global variable nor an iterative approach.
Actually, you do not return your node - the recursive call results are ignored. Rewrite it so it returns a Node:
Node klargest(Node root,int k,int count)
{
Node result = null;
if(root != null)
{
result = klargest(root.right,k,count);
if (result != null)
return result;
count++;
if(count == k)
return root;
result = klargest(root.left,k,count);
}
return result;
}
You can use this approach:
int kthLargest(Node node, int k) {
int rightCount = count(node.right);
if (k <= rightCount) {
return kthLargest(node.right, k);
} else if (k == rightCount+1) {
return node.data;
} else {
return kthLargest(node.left, k - rightCount + 1);
}
}
int count(Node node) {
if (node != null) {
return count(node.left) + count(node.right) + 1;
}
return 0;
}

Issue with Recursive Methods ("missing return statement")

so I have a program that is running a bunch of different recursive methods, and I cannot get it to compile/run. The error is in this method, according to my computer:
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n==0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else if (n>1)
{
return fibo(n-1) + fibo(n-2);
}
}
I have this method called correctly in my main method, so the issue is in this bit of code.
I think I can help you in this. Add return n; after your else if. Outside of the code but before the last curlicue.
The code will work as long as n ≥ 0 btw; another poster here is right in that you may want to add something to catch that error.
Make sure all possible paths have a return statement. In your code, if n < 0, there is no return statement, the compiler recognizes this, and throws the error.
public static int fibo(int n)
// returns the nth Fibonacci number
{
if (n<=0)
{
return 0;
}
else if (n==1)
{
return 1;
}
else // All other cases, i.e. n >= 1
{
return fibo(n-1) + fibo(n-2);
}
}

How do you pass different collections through the same parameter?

I am creating a method that will take a collection of different types (divs, spans..) and search it. I can't find a parameter to pass different collection types though. I tried IElementContainer and ElementCollections but I can't cast DivCollection to either. Is there another way?
The method that does the search:
private static ElementCollection searchCollections(IElementContainer ec, WACore.compStringInfo info)
{
if (info.componentIDName == WACore.componentIDs.Id.ToString())
{
return ec.Elements.Filter(Find.ById(info.componentIDValue));
}
else if (info.componentIDName == WACore.componentIDs.Name.ToString())
{
return ec.Elements.Filter(Find.ByName(info.componentIDValue));
}
else if (info.componentIDName == WACore.componentIDs.Title.ToString())
{
return ec.Elements.Filter(Find.ByTitle(info.componentIDValue));
}
else if (info.componentIDName == WACore.componentIDs.OuterText.ToString())
{
String str = info.componentIDValue.Substring(1, 6);
return ec.Elements.Filter(Find.ByText(new Regex(str)));
}
else
{
return null;
}
}
The caller(s):
return searchCollections((IElementContainer)doc.TextFields, info);
return searchCollections((IElementContainer)(doc.Divs), info);
return searchCollections((IElementContainer)doc.Spans, info);

How to match text in string in Arduino

I have some issues with Arduino about how to match text.
I have:
String tmp = +CLIP: "+37011111111",145,"",,"",0
And I am trying to match:
if (tmp.startsWith("+CLIP:")) {
mySerial.println("ATH0");
}
But this is not working, and I have no idea why.
I tried substring, but the result is the same. I don't know how to use it or nothing happens.
Where is the error?
bool Contains(String s, String search) {
int max = s.length() - search.length();
for (int i = 0; i <= max; i++) {
if (s.substring(i) == search) return true; // or i
}
return false; //or -1
}
Otherwise you could simply do:
if (readString.indexOf("+CLIP:") >=0)
I'd also recommend visiting:
https://www.arduino.cc/en/Reference/String
I modified the code from gotnull. Thanks to him to put me on the track.
I just limited the search string, otherwise the substring function was not returning always the correct answer (when substrign was not ending the string). Because substring search always to the end of the string.
int StringContains(String s, String search) {
int max = s.length() - search.length();
int lgsearch = search.length();
for (int i = 0; i <= max; i++) {
if (s.substring(i, i + lgsearch) == search) return i;
}
return -1;
}
//+CLIP: "43660417XXXX",145,"",0,"",0
if (strstr(command.c_str(), "+CLIP:")) { //Someone is calling
GSM.print(F("ATA\n\r"));
Number = command.substring(command.indexOf('"') + 1);
Number = Number.substring(0, Number.indexOf('"'));
//Serial.println(Number);
} //End of if +CLIP:
This is how I'm doing it. Hope it helps.
if (tmp.startsWith(String("+CLIP:"))) {
mySerial.println("ATH0");
}
You can't put the string with quotes only you need to cast the variable :)

Resources