0

我需要制作一个在 api 中搜索名称的应用程序。问题是表单小部件没有发送我在输入中写的名称。

重要的部分是:dadosPessoa()Form()两者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']);
                        },
                    ),
                  ],
                ),
              ),
            );
          }
        }

在此处输入图像描述

4

1 回答 1

1

好的,我在您的代码中修复了很多东西,这应该看起来像这样

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();
              },
            ),
          ],
        ),
      ),
    );
  }
}

您首先将变量分配给空字符串,因为name1返回空表单的当前值。sobrenome1TextEditingController().text

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

希望这会帮助你。

于 2020-06-11T22:27:35.530 回答