首页 > 解决方案 > 如何将 TextField 文本设置为从 JSON 文件接收的文本?(扑)

问题描述

我对颤动很陌生,我想知道如何使用TextEditController(名为 crText)将 TextField 文本设置为从 JSON 文件接收的文本。JSON 文件中的文本存储在 chemText 中,它是一个字符串。在“搜索”图标中的方法期间getData调用具有所有查询功能的方法。onPressed需要使用设置为 crText 的“控制器:”更改 TextField 中的文本。这里的目标是将 crText 设置为 chemText(它保存来自 queryData 变量 ( json.decode(queryParams)) 的文本。“控制器:”可以在代码段的最后找到。

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,

            )
          ],
        ),
      ),
    );
  }

}


标签: jsonflutter

解决方案


添加

setState(() { crText.text = chemText });

某处。也许在解析你的json之后。


推荐阅读