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

Time:09-21

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;
    }
  }
}

CodePudding user response:

Try this please

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

CodePudding user response:

I don't quite understand what you want to achieve, but maybe you need to add an asynchronous method like ticketModel = await HttpVerifyTicketPost( //add await eventId: widget.eventID, ticket: ticket, scannerId: widget.scannerId, ).verifyTicket(); and you must add async like Future Foo() async {your code...}

  • Related