2
I need to search inside this Json
what I type into TextField
The controller:
TextEditingController searchController = TextEditingController();
String variavel_json = '{"nome":"Marcelo", "email":"[email protected]"}';
The search function:
void printJson() {
setState(() {
Map json = jsonDecode(variavel_json);
if(json["nome"] == searchController){
print(json["nome"]);
}else{
print("Nenhum nome encontrado");
}
});
}
The TextField
:
TextField(
keyboardType: TextInputType.text,
decoration: InputDecoration(
labelText: searchText,
labelStyle: TextStyle(color: Colors.blue),
),
textAlign: TextAlign.center,
controller: searchController,
),
And the button that calls the controller
:
ButtonTheme(
height: 30.0,
child: RaisedButton(
onPressed: printJson,
shape: new RoundedRectangleBorder(
borderRadius: new BorderRadius.circular(30.0)),
child: Text(
"Printar JSON",
style: TextStyle(color: Colors.white, fontSize: 30),
), //Text
color: Colors.blue,
), //RaisedButton
),
The return I have on the console is always the else
. I would like to understand how I could do this research within the Map. I am new to the language.
I understand, but in this case you are assigning the nameOne to Marcelo so the comparison will always work. How I would pick up the text the user types to make the comparison ?
– Marcelo L
It will always work because it is an example, if you see there in the answer, a little above, has how you should do
if(json["nome"] == searchController.text){
. Using the searchController text.– Julio Henrique Bitencourt
Ah yes, but even with this property
text
the search still falls onelse
– Marcelo L
What value is printing on
text
? Debug and inspect this variable– Julio Henrique Bitencourt
How could I do this debug ? has some tool or something like php with a kind of
var_dump($variavel)
?– Marcelo L
You can use the functions
debugPrint
orprint
to simply print the values. Or use your IDE to run in debug mode and be able to inspect the variables. Further information: https://flutter.dev/docs/testing/debugging– Julio Henrique Bitencourt
I managed to find the error here. After printing what was on
searchController
I noticed an error in the Json syntax. Thanks for the help– Marcelo L
Just one more question, when I add another user with email name always picks up the last one. Any idea what this might be ?
– Marcelo L
@Marcelol in this case you are architecting your problem wrong, a Map will never allow a
key
duplicate. Only possible with different Maps or a list of values for the key.– Julio Henrique Bitencourt