我需要在 Flutter 中一个接一个 运行 两个函数
I need to run two functions one after another in Flutter
基本上我有一个 flutter 项目,我必须在其中解析来自 REST API 的 JSON 数据。首先,我必须执行 Post 请求才能获得“UUID”。然后使用此参数(UUID)创建 GET 请求以使用 UUID 并再次使用 REST API 获取订单信息。我创建了两个按钮,一个用于 POST 请求,另一个用于 GET 请求。现在我要做的是通过单击一个按钮来执行这两个操作(第一个 POST、第二个 GET)。我被这个问题困住了。也许我应该使用 VoidCallBack 函数?或者其他方法?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'ordertaxi_model.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Map uuid;
String data;
void orderTaxi() async {
var data = {
'api_key': 'this is my API key, sorry can not show',
'phone': '1111',
'street': 'Testttttt',
'house': '11',
'appartment': '11',
'dst': 'Testtttttt',
'moderation_required': 'yes',
};
var res = await http.post('rest api can not show', body: data);
uuid = json.decode(res.body);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(uuid['response']['uuid']);
}
Future<String> orderInfo() async {
var params = {
'api_key': 'rest api can not show',
'uuid': uuid['response']['uuid'],
};
var query = params.entries.map((p) => '${p.key}=${p.value}').join('&');
var res = await http.get('rest api can not show?$query');
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
var info = json.decode(res.body);
setState(() {
String data = info['response']['message'];
print(data);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('X-Fit Test'),),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed:(){
orderTaxi();
},
),
body:
您可以通过在第一个函数的末尾添加第二个函数 orderInfo()
来做到这一点
void orderTaxi() async {
var data = {
'api_key': 'this is my API key, sorry can not show',
'phone': '1111',
'street': 'Testttttt',
'house': '11',
'appartment': '11',
'dst': 'Testtttttt',
'moderation_required': 'yes',
};
var res = await http.post('rest api can not show', body: data);
uuid = json.decode(res.body);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(uuid['response']['uuid']);
orderInfo(); // Add it here
}
Future<String> orderInfo() async {
var params = {
'api_key': 'rest api can not show',
'uuid': uuid['response']['uuid'],
};
var query = params.entries.map((p) => '${p.key}=${p.value}').join('&');
var res = await http.get('rest api can not show?$query');
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
var info = json.decode(res.body);
setState(() {
String data = info['response']['message'];
print(data);
});
}
或
您可以等待第一个,然后再调用 onPressed
中的第二个,就像这样
onPressed:() async {
await orderTaxi();
orderInfo();
},
基本上我有一个 flutter 项目,我必须在其中解析来自 REST API 的 JSON 数据。首先,我必须执行 Post 请求才能获得“UUID”。然后使用此参数(UUID)创建 GET 请求以使用 UUID 并再次使用 REST API 获取订单信息。我创建了两个按钮,一个用于 POST 请求,另一个用于 GET 请求。现在我要做的是通过单击一个按钮来执行这两个操作(第一个 POST、第二个 GET)。我被这个问题困住了。也许我应该使用 VoidCallBack 函数?或者其他方法?
import 'dart:convert';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'ordertaxi_model.dart';
class HomePage extends StatefulWidget {
@override
_HomePageState createState() => _HomePageState();
}
class _HomePageState extends State<HomePage> {
Map uuid;
String data;
void orderTaxi() async {
var data = {
'api_key': 'this is my API key, sorry can not show',
'phone': '1111',
'street': 'Testttttt',
'house': '11',
'appartment': '11',
'dst': 'Testtttttt',
'moderation_required': 'yes',
};
var res = await http.post('rest api can not show', body: data);
uuid = json.decode(res.body);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(uuid['response']['uuid']);
}
Future<String> orderInfo() async {
var params = {
'api_key': 'rest api can not show',
'uuid': uuid['response']['uuid'],
};
var query = params.entries.map((p) => '${p.key}=${p.value}').join('&');
var res = await http.get('rest api can not show?$query');
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
var info = json.decode(res.body);
setState(() {
String data = info['response']['message'];
print(data);
});
}
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(title: Text('X-Fit Test'),),
floatingActionButton: FloatingActionButton(
child: Icon(Icons.add),
onPressed:(){
orderTaxi();
},
),
body:
您可以通过在第一个函数的末尾添加第二个函数 orderInfo()
来做到这一点
void orderTaxi() async {
var data = {
'api_key': 'this is my API key, sorry can not show',
'phone': '1111',
'street': 'Testttttt',
'house': '11',
'appartment': '11',
'dst': 'Testtttttt',
'moderation_required': 'yes',
};
var res = await http.post('rest api can not show', body: data);
uuid = json.decode(res.body);
if (res.statusCode != 200) throw Exception('http.post error: statusCode= ${res.statusCode}');
print(uuid['response']['uuid']);
orderInfo(); // Add it here
}
Future<String> orderInfo() async {
var params = {
'api_key': 'rest api can not show',
'uuid': uuid['response']['uuid'],
};
var query = params.entries.map((p) => '${p.key}=${p.value}').join('&');
var res = await http.get('rest api can not show?$query');
if (res.statusCode != 200) throw Exception('http.get error: statusCode= ${res.statusCode}');
var info = json.decode(res.body);
setState(() {
String data = info['response']['message'];
print(data);
});
}
或
您可以等待第一个,然后再调用 onPressed
中的第二个,就像这样
onPressed:() async {
await orderTaxi();
orderInfo();
},