0

I need to make an app which searches a name in an api. The problem is that the form widget doesn't send the name I write on the input.

The part that matters are: dadosPessoa(), Form() and the both TextFormField()

        import 'dart:convert';
        import 'package:flutter/cupertino.dart';
        import 'package:flutter/material.dart';
        import 'package:async/async.dart';
        import 'package:http/http.dart' as http;
        import 'package:convert/convert.dart';

        var nome1 = TextEditingController();
        var sobrenome1 = TextEditingController();
        var nome = nome1.text;
        var sobrenome = sobrenome1.text;
        String responsebody;


        dynamic url = "https://completecriminalchecks.com/api/json/?firstname=$nome&lastname=$sobrenome&apikey=fooberry";

        Future<void> main() async {
          http.Response response = await http.get(url);
          responsebody = response.body;

          Future<String> dadosPessoa() async {
            print(json.decode(responsebody)['person']);
          }

          runApp(MaterialApp(
            home: _Home(),
          ));
        }

        class _Home extends StatefulWidget {
          @override

          __HomeState createState() => __HomeState();
        }

        class __HomeState extends State<_Home> {
          String get resultado => null;

          @override
          Widget build(BuildContext context) {
            return Scaffold(
              appBar: AppBar(
                title: Text(
                    "FIVE-0"),
              ),

              body:
              Form(
                child:
                Column(
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: <Widget>[
                    TextFormField(
                      controller: nome1,
                      decoration: const InputDecoration(
                        hintText: "Nome do suspeito:",

                      ),
                      validator: (value) {
                        if (value.isEmpty) {
                          return 'Digite o nome sugeito';
                        }
                        return null;
                      },
                    ),


                    TextFormField(
                      controller: sobrenome1,
                      decoration: const InputDecoration(
                        hintText: "Nome do suspeito:",

                      ),
                      validator: (value) {
                        if (value.isEmpty) {
                          return 'Digite o nome sugeito';
                        }
                        return null;
                      },
                    ),
                    RaisedButton(
                      onPressed: () {
                        print(json.decode(responsebody)['person']);
                        },
                    ),
                  ],
                ),
              ),
            );
          }
        }

enter image description here

halfer
  • 19,824
  • 17
  • 99
  • 186
Theo2017BR
  • 11
  • 2
  • don't post your apiKey online. – returnVoid Jun 11 '20 at 20:08
  • Please take care to ensure that your post looks like you intended. The code snippet here had your image right in the middle, and this would have been very evident from the preview window (as well as what the post looked like after publishing). Thus, the image did not render. – halfer Jun 11 '20 at 23:16
  • Also, titles here (and indeed anywhere on the internet) need to be informative. They are like a little advertisement to say what the question is above. "Halp me" and "Need a kind soul" is not useful. Explain what your question is about, so you can attract subject matter experts, and so future readers can see this question if it is helpful for their problem. – halfer Jun 11 '20 at 23:18

1 Answers1

1

Okay i fixed lots of things in your code and this should looks like this

import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;

void main() async {
  runApp(MaterialApp(
    home: _Home(),
  ));
}

class _Home extends StatefulWidget {
  @override
  __HomeState createState() => __HomeState();
}

class __HomeState extends State<_Home> {
  final nome1 = TextEditingController();
  final sobrenome1 = TextEditingController();
  String responsebody;

  void getData() async {
    String url =
        "https://completecriminalchecks.com/api/json/?firstname=${nome1.text}&lastname=${sobrenome1.text}&apikey=fooberry";
    http.Response response = await http.get(url);
    responsebody = response.body;

    print(json.decode(responsebody)['person']);
  }

  String get resultado => null;

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text("FIVE-0"),
      ),
      body: Form(
        child: Column(
          crossAxisAlignment: CrossAxisAlignment.center,
          children: <Widget>[
            TextFormField(
              controller: nome1,
              decoration: const InputDecoration(
                hintText: "Nome do suspeito:",
              ),
              validator: (value) {
                if (value.isEmpty) {
                  return 'Digite o nome sugeito';
                }
                return null;
              },
            ),
            TextFormField(
              controller: sobrenome1,
              decoration: const InputDecoration(
                hintText: "Nome do suspeito:",
              ),
              validator: (value) {
                if (value.isEmpty) {
                  return 'Digite o nome sugeito';
                }
                return null;
              },
            ),
            RaisedButton(
              child: Text("Submit"),
              onPressed: () {
                getData();
              },
            ),
          ],
        ),
      ),
    );
  }
}

First thing you have assigned name1 and sobrenome1 variable to a empty String because TextEditingController().text returns the current value of the form which is empty.

nome1 = TextEditingController();
 var nome = nome1.text;

Hope this will help you.

Sachin Bhankhar
  • 900
  • 8
  • 14