我创建了这个类来获取api,我不能从我的textinput另一个类获取textForm。
class RemoteService {
Future<List<Member>?> getUser({String? textForm}) async {
var clientuser = http.Client();
var uriuser = Uri.parse('HTTP://MY URI//$textForm');
var responseuse = await clientuser.get(uriuser);
if (responseuse.statusCode == 200) {
var json = responseuse.body;
return memberFromJson(json);
}
}
}
主类,我尝试发送textFieldValue。文本到RemoteService获取数据,但当我编译我的代码,它不能得到我的值从我的文本字段。
class MyCustomFormState extends State<MyCustomForm> {
List<Member>? user1;
var isLoaded = false;
TextEditingController textFieldValue= TextEditingController();
late String getusevalue;
@override
void initState() {
super.initState();
getUse();
}
getUse() async {
user1 = await RemoteService().getUser(textForm: textFieldValue.text);
if (user1 != null) {
setState(() {
isLoaded = true;
});
}
}
@override
Widget build(BuildContext context) {
Container(
width: 200,
child:
TextFormField(
controller: textFieldValue,
decoration: InputDecoration(
border: OutlineInputBorder(),
fillColor: Color.fromARGB(255, 255, 255, 255),
hintText: 'PLEASE ENTER ID',
),
onChanged: (text) {
getusevalue = textFieldValue.text;
},
)
),
Container(
width: 120,
height: 50,
margin: EdgeInsets.only(top: 15),
child: ElevatedButton(
child: Text("LOGIN",style: TextStyle(
fontSize: 16, fontWeight: FontWeight.bold),),
style: ElevatedButton.styleFrom(
primary: Color.fromARGB(255, 45, 160, 253),
elevation: 0,
), onPressed: () {
print("INPUT = "+getusevalue);
print("Fetch API = ${user1?[0].name}");
},
)
),
错误:
Performing hot restart...
Restarted application in 2,712ms.
E/flutter (13579): [ERROR:flutter/runtime/dart_vm_initializer.cc(41)] Unhandled Exception: type '(dynamic) => Member' is not a subtype of type '(String, dynamic) => MapEntry<dynamic, dynamic>' of 'transform'
E/flutter (13579): #0 memberFromJson (package:flutter_exit_interview/dtbmember.dart:8:79)
E/flutter (13579): #1 RemoteService.getUser (package:flutter_exit_interview/main.dart:1061:14)
E/flutter (13579): <asynchronous suspension>
E/flutter (13579): #2 MyCustomFormState.getUse (package:flutter_exit_interview/main.dart:83:13)
E/flutter (13579): <asynchronous suspension>
E/flutter (13579):
这是我的模型:
// To parse this JSON data, do
//
// final member = memberFromJson(jsonString);
import 'package:meta/meta.dart';
import 'dart:convert';
List<Member> memberFromJson(String str) => List<Member>.from(json.decode(str).map((x) => Member.fromJson(x)));
String memberToJson(List<Member> data) => json.encode(List<dynamic>.from(data.map((x) => x.toJson())));
class Member {
Member({
required this.status,
required this.personId,
required this.code,
required this.name,
required this.nickname,
required this.deptcode,
});
String status;
String personId;
String code;
String name;
String nickname;
String deptcode;
factory Member.fromJson(Map<String, dynamic> json) => Member(
status: json["status"],
personId: json["person_id"],
code: json["code"],
name: json["name"],
nickname: json["nickname"],
deptcode: json["deptcode"],
);
Map<String, dynamic> toJson() => {
"status": status,
"person_id": personId,
"code": code,
"name": name,
"nickname": nickname,
"deptcode": deptcode,
};
}
尝试以下更改:
String getusevalue = '';
setState(() {
getusevalue = textFieldValue.text;
});
现在我试试它的工作。
getUse() async {
String getusevalue = 'P3116'; //setvalue not from my input
user1 = await RemoteService().getUser(textForm: '${getusevalue}');
if (user1 != null) {
setState(() {
isLoaded = true;
});
}
}
但上次我试着这样做,它不工作:
getUse() async {
String getusevalue = textFieldValue.text; //set value from my input!
user1 = await RemoteService().getUser(textForm: '${getusevalue}');
if (user1 != null) {
setState(() {
isLoaded = true;
});
}
}