如何将TextField文本设置为从JSON文件接收的文本? (扑)

时间:2019-11-26 22:53:33

标签: json flutter

我很陌生,我想知道如何使用TextEditController(名为crText)将TextField文本设置为从JSON文件接收的文本。 JSON文件中的文本存储在字符串形式的chemText中。具有所有查询功能的getData方法在“搜索”图标的onPressed方法期间被调用。希望使用设置为crText的“ controller:”来更改TextField中的文本。这里的目标是将crText设置为chemText(用于保存来自queryData变量(json.decode(queryParams)的文本)。“ controller:”可以在代码段的末尾找到。

import 'dart:ffi';
import 'dart:io';

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


class myClass extends StatelessWidget{
  var crText = TextEditingController();
  String chemText;

  var queryParameters = {
    "ID" :  "nfuiwnfuiwnfuinfuwnfuinw" //just one of the hashes (example)
  };


  Future<String> getData() async{


    var uri =
    Uri.https('www.myUrl.com', 'api/parameters', queryParameters);

    Icon searchIcon = Icon(Icons.search);
    var objMapUrl = "https://myUrl.com";
    Map data;
    Map queryData;
    http.Response response  = await http.get(
        Uri.encodeFull(objMapUrl),
        headers: {

          "key" : "ifhfi2jfowjefounf893f"
        }
    );

    var queryParams = await http.get(uri, headers: {
      "key" : "ifhfi2jfowjefounf893f"

    });

    //data = json.decode(response.body);

    //print(data["hashes"]);
    // print( data["names"]);
    // print(data); //prints hashes

    //query parameters

    queryData = json.decode(queryParams.body);
    //print(queryData);

    chemText = queryData.toString();
    print(chemText);

    return chemText;

  }

  @override
  Widget build(BuildContext context) {

    return Scaffold(
      appBar: AppBar(
        backgroundColor: Colors.white,
        elevation: 0.0,
        automaticallyImplyLeading: false,
        title: Container(
          margin: EdgeInsets.symmetric(horizontal: 1.0, vertical: 8.0),
          decoration: BoxDecoration(
            color: Colors.lightBlueAccent,
            borderRadius: BorderRadius.all(Radius.circular(12.0)),

          ),
          child: Row(
            crossAxisAlignment: CrossAxisAlignment.start,
            children: <Widget>[
              Expanded(
                flex: 1,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 5.0),
                  child: TextFormField(

                    textInputAction: TextInputAction.done,
                    onFieldSubmitted: (term){
                        getData();
                    },
                    cursorColor: Colors.white,
                    decoration: InputDecoration(
                      border: InputBorder.none,
                      hintText: "Enter  Name",
                      hintStyle: TextStyle(color: Colors.white),
                    ),
                  ),
                ),
              ),
              Expanded(
                flex: 0,
                child: Container(
                  padding: EdgeInsets.symmetric(horizontal: 0.0),
                  child: Row(
                    children: <Widget>[
                      IconButton(
                        onPressed: getData,
                        icon: Icon(Icons.search, color: Colors.white),
                      ),
                    ],
                  ),
                ),
              ),
            ],
          ),
        ),
      ),
      body: new Container(
        padding:  new EdgeInsets.fromLTRB(25.0, 200.0, 25.0, 0.0),

        child: Column(
          crossAxisAlignment: CrossAxisAlignment. center,
          children: <Widget>[
            new TextField(
              decoration: new InputDecoration(
                border: const OutlineInputBorder(
                  borderSide: const BorderSide(
                    color: Colors.blueGrey, width: 2.0
                  )
                )
              ),
             //textfield text needs to change to the text recieved from json.decode(queryParams.body) or chemText
             controller: crText,
              maxLines: 20,

            )
          ],
        ),
      ),
    );
  }

}


1 个答案:

答案 0 :(得分:0)

添加

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <input type="checkbox" id="check-all"> <input type="checkbox" class="check"> <input type="checkbox" class="check"> <input type="checkbox" class="check">

某处。也许是在解析了json之后。