【问题标题】:I need to run two functions one after another in Flutter我需要在 Flutter 中一个接一个地运行两个函数
【发布时间】:2020-07-16 13:24:21
【问题描述】:

基本上我有一个颤振项目,我必须从 REST API 解析 JSON 数据。首先,我必须执行 Post Request 以获得“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:

【问题讨论】:

    标签: flutter flutter-layout flutter-dependencies


    【解决方案1】:

    您可以通过在第一个函数的末尾添加第二个函数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();
    },
    

    【讨论】:

    • 谢谢你,你救了我
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-28
    • 1970-01-01
    • 2022-11-22
    • 2018-08-30
    • 2019-05-22
    • 2014-07-20
    相关资源
    最近更新 更多