json - 如何将 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,
)
],
),
),
);
}
}
解决方案
添加
setState(() {
crText.text = chemText
});
某处。也许在解析你的json之后。
推荐阅读
- c# - Azure 存储队列 - 消息 ID
- javascript - 删除对象中的重复值,但将值存储在数组中
- acumatica - 如何将自定义字段值从销售订单屏幕传递到 acumatica 代码中的销售发票屏幕
- java - 有没有办法在硒中自动化或绕过谷歌验证码?
- python - 使用第 1 列和第 2 列填充第 3 列
- java - Java 中的 Xpath 过滤器仅返回第一个值结果
- animation - 使用关键帧耀斑编辑顶点
- websocket - Dart 将客户端 Socket 升级为 WebSocket
- angularjs - 如何在 ng-repeat 中迭代 2 组数据
- java - 多模块项目中的 Maven 构建中的循环引用