Flutter // 单击按钮使用异步执行两个函数并等待函数 onPressed 并从 api 获取数据
Flutter // click button to execute two function using async and wait on function onPressed and get data from api
尝试在 TextField 和 onPressed 上的局部变量上设置一些值,以获取 TextField 上给定的局部变量和第二个变量,同时单击 onPressed 以使用两个函数,一个是 doSomething,另一个是 fetchBalance 函数。
第一个函数 doSomthig 工作正常,但 fetchBalance 函数响应主体打印成功,但内部条件未执行,但程序代码未给出任何错误。现在,我坚持这个请帮助我。
这是我们的变量
var _addressControler= TextEditingController();
var url1='This is default variable ';
var url2='noAddress entered';
第一个函数
doSomething(){
setState(() {
if(_addressControler.text == _addressControler.text){
url2 = _addressControler.text.toString();
}else{
url2=url2;
print("$url2");
}
print("$url2");
});
}
这是余额的局部变量
late Future<Balance> futureBalance;
final _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
futureBalance = fetchBalance();
}
Future<Balance> fetchBalance() async {
print("$url1");
print("$url2");
var finalAddress=url1+url2;
print("$finalAddress");
final response =
await http.get(Uri.parse(finalAddress));
print(response.body);
if (response.statusCode == 200) {
return Balance.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
@override Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.amberAccent,
appBar: AppBar(
title: const Text('Retrieve Text Input'),
),
body: Padding(
key: _formKey,
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
controller: _addressControler,
decoration: InputDecoration(labelText: 'Enter the address...',
labelStyle: TextStyle(color: Colors.blue),border: new OutlineInputBorder(
borderSide:new BorderSide(color: Colors.black)),),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed:() async{
doSomething();
await fetchBalance();
},
child: const Text('Submit'),
throw Exception('Failed to load album');
}
}
这是我们的ui基地
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.amberAccent,
appBar: AppBar(
title: const Text('Retrieve Text Input'),
),
body: Padding(
key: _formKey,
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
controller: _addressControler,
decoration: InputDecoration(labelText: 'Enter the address...',
labelStyle: TextStyle(color: Colors.blue),border: new OutlineInputBorder(
borderSide:new BorderSide(color: Colors.black)),),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed:() async{
doSomething();
await fetchBalance();
},
child: const Text('Submit'),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Text("$url2"),
),
Container(
child: FutureBuilder<Balance>(
future: futureBalance,
builder: (context, snapshot) {
if (snapshot.hasData) {
print(snapshot.data!.height);
print(snapshot.data!.result[0].amount);
var x = (snapshot.data!.result[0].amount);
var y =int.parse(x);
assert(y is int);
),
],
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
),
],
),
)
);
}
}
print(z);
return
Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('${z.toString()}',style: TextStyle(fontSize: 20,),),
],
),
),
],
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
),
],var _addressControler= TextEditingController();
),
)
);
}
}
您不需要 url2 变量,只需在 fetchBalance()
中发送 _addressControler.text
Future<Balance> fetchBalance() async {
final response =
await http.get(Uri.parse(url1 +_adressController.text));
if (response.statusCode == 200) {
return Balance.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
正在运行...
child: ElevatedButton(
onPressed: () {
setState(() {
fetchBalance();
});
fetchBalance();
},
child: const Text('Submit'),
),
),
尝试在 TextField 和 onPressed 上的局部变量上设置一些值,以获取 TextField 上给定的局部变量和第二个变量,同时单击 onPressed 以使用两个函数,一个是 doSomething,另一个是 fetchBalance 函数。 第一个函数 doSomthig 工作正常,但 fetchBalance 函数响应主体打印成功,但内部条件未执行,但程序代码未给出任何错误。现在,我坚持这个请帮助我。
这是我们的变量
var _addressControler= TextEditingController();
var url1='This is default variable ';
var url2='noAddress entered';
第一个函数
doSomething(){
setState(() {
if(_addressControler.text == _addressControler.text){
url2 = _addressControler.text.toString();
}else{
url2=url2;
print("$url2");
}
print("$url2");
});
}
这是余额的局部变量
late Future<Balance> futureBalance;
final _formKey = GlobalKey<FormState>();
@override
void initState() {
super.initState();
futureBalance = fetchBalance();
}
Future<Balance> fetchBalance() async {
print("$url1");
print("$url2");
var finalAddress=url1+url2;
print("$finalAddress");
final response =
await http.get(Uri.parse(finalAddress));
print(response.body);
if (response.statusCode == 200) {
return Balance.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
@override Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.amberAccent,
appBar: AppBar(
title: const Text('Retrieve Text Input'),
),
body: Padding(
key: _formKey,
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
controller: _addressControler,
decoration: InputDecoration(labelText: 'Enter the address...',
labelStyle: TextStyle(color: Colors.blue),border: new OutlineInputBorder(
borderSide:new BorderSide(color: Colors.black)),),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed:() async{
doSomething();
await fetchBalance();
},
child: const Text('Submit'),
throw Exception('Failed to load album');
}
}
这是我们的ui基地
@override
Widget build(BuildContext context) {
return Scaffold(
backgroundColor: Colors.amberAccent,
appBar: AppBar(
title: const Text('Retrieve Text Input'),
),
body: Padding(
key: _formKey,
padding: const EdgeInsets.all(16.0),
child: Column(
children: [
Padding(
padding: const EdgeInsets.all(10.0),
child: TextField(
controller: _addressControler,
decoration: InputDecoration(labelText: 'Enter the address...',
labelStyle: TextStyle(color: Colors.blue),border: new OutlineInputBorder(
borderSide:new BorderSide(color: Colors.black)),),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: ElevatedButton(
onPressed:() async{
doSomething();
await fetchBalance();
},
child: const Text('Submit'),
),
),
Padding(
padding: const EdgeInsets.symmetric(vertical: 16.0),
child: Text("$url2"),
),
Container(
child: FutureBuilder<Balance>(
future: futureBalance,
builder: (context, snapshot) {
if (snapshot.hasData) {
print(snapshot.data!.height);
print(snapshot.data!.result[0].amount);
var x = (snapshot.data!.result[0].amount);
var y =int.parse(x);
assert(y is int);
),
],
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
),
],
),
)
);
}
}
print(z);
return
Column(
children: [
Padding(
padding: const EdgeInsets.all(16.0),
child: Row(
mainAxisAlignment: MainAxisAlignment.center,
children: [
Text('${z.toString()}',style: TextStyle(fontSize: 20,),),
],
),
),
],
);
} else if (snapshot.hasError) {
return Text("${snapshot.error}");
}
// By default, show a loading spinner.
return CircularProgressIndicator();
},
),
),
],var _addressControler= TextEditingController();
),
)
);
}
}
您不需要 url2 变量,只需在 fetchBalance()
_addressControler.text
Future<Balance> fetchBalance() async {
final response =
await http.get(Uri.parse(url1 +_adressController.text));
if (response.statusCode == 200) {
return Balance.fromJson(jsonDecode(response.body));
} else {
throw Exception('Failed to load album');
}
}
正在运行...
child: ElevatedButton(
onPressed: () {
setState(() {
fetchBalance();
});
fetchBalance();
},
child: const Text('Submit'),
),
),