How to assign values from an API call to a variable in flutter

385

Try this please

HttpVerifyTicketPost(
          eventId: widget.eventID,
          ticket: ticket,
          scannerId: widget.scannerId,
        ).verifyTicket().then((value){setState(() {
            ticketModel=value
          });
    });
Share:
385
oluwatyson
Author by

oluwatyson

Updated on January 01, 2023

Comments

  • oluwatyson
    oluwatyson over 1 year

    I have the following method which is use dto verify a ticket/token

        var ticketArray = ticket.split('|');
        //First check to verify token using simple versification algo
        if (widget.eventID.toString() != (ticketArray[0])) {
          setState(() {
            ticketMainMsg = 'This QR code is NOT VALID';
            ticketsubtitle = ticketArray.length != 2
                ? 'The QR code is fake'
                : 'QR code could belong to another event';
            ticketStatus = false;
            return;
          });
        }
        //Make API call
        ticketModel = HttpVerifyTicketPost(
          eventId: widget.eventID,
          ticket: ticket,
          scannerId: widget.scannerId,
        ).verifyTicket();
      }
    

    From above, you can see I do a very simple check on the qr code/token if this simple step fails, I don't bother making an API call and I set the state based on these values.

    However if the check passes, then I proceed to make an API call to the server to fully verify the token/code.

    My issue is I am struggling to now assign the values from the API call to the ticketStatus, ticketMainMsgand ticketsubtitle parameters. Can anyone helo shed some light. I am quite new to flutter but I am aware that the TicketModel will be a type of Future. My background is PHP so forgive me!

    EDIT: The httpVerifyTicket Class

    class HttpVerifyTicketPost {
      String ticket;
      int someId;
      int anotherId;
    
      HttpVerifyTicketPost(
          {required this.ticket, required this.someId, required this.anotherId});
    
      String verifyURL =
          'https://api.com/api/vendors/scanner/native/verify/ticket';
    
      Future<TicketModel> verifyTicket() async {
        var storage = await SharedPreferences.getInstance();
        var code= storage.getString('code');
    
        var client = http.Client();
        var ticketModel = null;
    
        var body = {
          'ticket': ticket,
          'scanner': scannerCode,
          'someId': someId,
          'anotherId': anotherId
        };
    
        try {
          var url = Uri.parse(verifyURL);
          var res = await client.post(url, body: jsonEncode(body));
          if (res.statusCode == 200) {
            var jsonString = res.body;
            var jsonMap = json.decode(jsonString);
            ticketModel = TicketModel.fromJson(jsonMap);
          }
          return ticketModel;
        } catch (Exception) {
          return ticketModel;
        }
      }
    }
    
    • Ananda Pramono
      Ananda Pramono over 2 years
      Can you show the code for HttpVerifyTicketPost
    • oluwatyson
      oluwatyson over 2 years
      @AnandaPramono I've now added it. Please check
    • nvoigt
      nvoigt over 2 years
      Does this answer your question? What is a Future and how do I use it?
  • oluwatyson
    oluwatyson over 2 years
    worked like a charm