Started by Kelsey RoySep 14, 2021

Open
How to set Api response message like {Success or Not - message} in UI / Screen in flutter?

0 VIEWES 0 LIKES 0 DISLIKES SHARE
0 LIKES 0 DISLIKES 0 VIEWES SHARE

I want to display message on screen or a snackbar or AlertDialog which get from api response.

So how to get API response message?

Here is my Function for Validate OTP as below in api_manager.dart file.

Future validateOtp(
context, String otp, LogedinUser? userData, UserPlan? userPlan) async {
final String otpUrl = "$baseUrl/otp/validate";
var formData = FormData.fromMap({
'user_id': userData!.userId,
'otp': otp,
'device_id': userData.deviceId,
});

var response = await dio.post(otpUrl, data: formData);
final validateOtpData = validateOtpFromJson(response.data);
final status = validateOtpData.success;

print(response.data);
if (response.statusCode == 200) {
var res = response.data;
print(userData.userId);
print(userData.deviceId);
print(otp);
print(res);
if (status == 1) {
Navigator.push(
context,
MaterialPageRoute(
builder: (context) => DashBoard(
user: userData,
userPlan: userPlan,
)));
}
}
}

Here is the response for invalid or expired OTP .

đź“·

And this is valid OTP response.

đź“·

Below is Validate OTP Model Class in validate_otp.dart file.

// To parse this JSON data, do
//
// final validateOtp = validateOtpFromJson(jsonString);

import 'dart:convert';

ValidateOtp validateOtpFromJson(String str) =>
ValidateOtp.fromJson(json.decode(str));

String validateOtpToJson(ValidateOtp data) => json.encode(data.toJson());

class ValidateOtp {
ValidateOtp({
required this.success,
required this.msg,
});

int success;
String? msg;

factory ValidateOtp.fromJson(Map<String, dynamic> json) => ValidateOtp(
success: json["success"] == null ? null : json["success"],
msg: json["msg"] == null ? null : json["msg"],
);

Map<String, dynamic> toJson() => {
// ignore: unnecessary_null_comparison
"success": success == null ? null : success,
"msg": msg == null ? null : msg,
};
}

0 Replies

You must be Logged in to reply
Techiio

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

facebooklinkdeintwitter

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Copyright techiio.com @2020 Kolkata, India
made with by Abhishek & Priyanka Jalan
Copyright techiio.com @2020
made with by Abhishek & Priyanka Jalan