When parsing the constructor GQLReq of type Hasura.GraphQL.Transport.HTTP.Protocol.GQLReq expected Object but got String - flutter

I am trying to make a request to a Hasura backend using Flutter, Chopper and Built Value and I am getting the following error
> When parsing the constructor GQLReq of type Hasura.GraphQL.Transport.HTTP.Protocol.GQLReq expected Object but got String.,
Chopper service
#ChopperApi(baseUrl: '/v1/graphql')
abstract class PostApiService extends ChopperService {
#Post()
Future<Response<BuiltPost>> get(#Body() String body);
static PostApiService create(AuthHeaderProvider authHeaderProvider) {
final client = ChopperClient(
baseUrl: 'https://arrivee-app-test.herokuapp.com',
services: [
_$PostApiService(),
],
converter: BuiltValueConverter(),
interceptors: [
HttpLoggingInterceptor(),
// HeadersInterceptor({'content-type': 'application/json'}),
HeadersInterceptor({'Authorization':'Bearer token'})
],
);
return _$PostApiService(client);
}
}
I make request with the following code
var request = RequestModel((b) => b
..query = fetchAccommodations()
..variables = null
..operationName = 'AccommodationGet');
var response = await client.get(request.toJson());
RequestModel
abstract class RequestModel
implements Built<RequestModel, RequestModelBuilder> {
String get query;
#nullable
String get variables;
String get operationName;
RequestModel._();
factory RequestModel([updates(RequestModelBuilder b)]) = _$RequestModel;
String toJson() {
return json
.encode(serializers.serializeWith(RequestModel.serializer, this));
}
static RequestModel fromJson(String jsonString) {
return serializers.deserializeWith(
RequestModel.serializer, json.decode(jsonString));
}
static Serializer<RequestModel> get serializer => _$requestModelSerializer;
}

Encountered the same error while trying to make a simple http POST call to Hasura in Angular Dart (no GraphQL client available).
I have found that the Json String had to be built in the following way :
String query = """
{
account {
id
}
}
""";
Map<String, dynamic> variables;
if (query.trimLeft().split(' ')[0] != 'query') {
query = 'query $docQuery';
}
var jsonMap = {'query': query, 'variables': variables};
final _headers = {'Content-Type': 'application/json'};
return _authHttpService.post(_apiUrl,
headers: _headers, body: json.encode(jsonMap));
Hope it helps anyone looking for a vanilla Dart solution.

I managed to solve it. posting here for future reference.
I let chopper do the conversion from a model to a string instead of doing it manually beforehand here
I changed the signature of the chopper service from :
#Post()
Future<Response<BuiltPost>> get(#Body() String body);
to
#Post()
Future<Response<BuiltPost>> get(#Body() RequestModel body);
and calling the service from:
var request = RequestModel((b) => b
..query = fetchAccommodations()
..variables = null
..operationName = 'AccommodationGet');
var response = await client.get(request.toJson());
to
var request = RequestModel((b) => b
..query = fetchAccommodations()
..variables = null
..operationName = 'AccommodationGet');
var value = await client.get(request);

Related

Flutter - loop not working while parsing json

I am trying to create model and parse json data from api
for that i created the model class you can see below
class FeatureModel {
String? PlanFeatures;
bool? FeatureStatus;
FeatureModel({this.PlanFeatures, this.FeatureStatus});
FeatureModel.fromJson(parsonJson) {
PlanFeatures = parsonJson['PlanFeatures'];
FeatureStatus = parsonJson['FeatureStatus'];
}
}
now i am trying to parse json with the help of loop
let me show you my method
List<FeatureModel> featureModel = [];
Uri featureAPI = Uri.parse(
planFeatureApi);
apiCall() async {
try {
http.Response response = await http.get(featureAPI);
// print(response.statusCode);
if (response.statusCode == 200) {
var decode = json.decode(response.body);
print(decode);
for (var i = 0; i < decode.length; i++) {
print(i);
featureModel.add(
FeatureModel.fromJson(decode[i]),
);
}
}
} catch (e) {}
}
I am calling it here
onPressed: () async{
await apiCall();
}
but the problem is here
loop is not working while parsing data
in that particular code i remains on 0 only
when i removes featureModel.add( FeatureModel.fromJson(decode[i]), ); i started increaing till 10
please let me know if i am making any mistake or what
thanks in advance
Here is the sample of api respone
[{"PlanFeatures":"Video Link Sharing","FeatureStatus":"true"},{"PlanFeatures":"Email \u0026amp; Telephonic Support","FeatureStatus":"true"},{"PlanFeatures":"Remove Pixeshare Branding","FeatureStatus":"false"},{"PlanFeatures":"Add Custom logo on uploaded photos","FeatureStatus":"false"},{"PlanFeatures":"Get Visitor Info","FeatureStatus":"false"},{"PlanFeatures":"Mobile Apps","FeatureStatus":"false"},{"PlanFeatures":"Send Questionnaries","FeatureStatus":"false"},{"PlanFeatures":"Create \u0026amp; Send Quotation","FeatureStatus":"false"},{"PlanFeatures":"Online Digital Album Sharing","FeatureStatus":"false"},{"PlanFeatures":"Analytics","FeatureStatus":"false"}]
thanks
I found many errors, first, the fromJson is not a factory constructor and doesn't return a class instance from the JSON.
the second one is that the bool values from the sample you added are String not a bool so we need to check over it.
try changing your model class to this:
class FeatureModel {
String? PlanFeatures;
bool? FeatureStatus;
FeatureModel({this.PlanFeatures, this.FeatureStatus});
factory FeatureModel.fromJson(parsonJson) {
return FeatureModel(
PlanFeatures: parsonJson['PlanFeatures'],
FeatureStatus: parsonJson['FeatureStatus'] == "false" ? false : true,
);
}
}

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

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;
}
}
}
Try this please
HttpVerifyTicketPost(
eventId: widget.eventID,
ticket: ticket,
scannerId: widget.scannerId,
).verifyTicket().then((value){setState(() {
ticketModel=value
});
});
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...}

Flutter - 'List<dynamic>' is not a subtype of type

I've been looking for a WordPress + Flutter App integration and found a good one, but I got this error message:
I'm pretty this is a simple error, but I'm more into a design guy than a dev, so would be great if some of you give me some tip about it. Thanks in advance!
import 'dart:convert';
import 'package:http/http.dart' as http;
import '../config.dart';
import '../model/post_entity.dart';
class WpApi {
static const String BASE_URL = URL + REST_URL_PREFIX + '/wp/v2/';
static Future<List<PostEntity>> getPostsList(
{int category = 0, int page = 1}) async {
var posts = [];
try {
String extra = category != 0 ? '&categories=' + '$category' : '';
dynamic response = await http.get(Uri.parse(BASE_URL +
'''
posts?_embed&page=$page''' +
extra));
dynamic json = jsonDecode(response.body);
(json as List).forEach((v) {
posts.add(PostEntity.fromJson(v));
});
} catch (e) {
//TODO Handle No Internet Response
}
return posts;
}
static Future<List<PostCategory>> getCategoriesList({int page = 1}) async {
List<PostCategory> categories = [];
try {
dynamic response = await http.get(Uri.parse(BASE_URL +
'categories?orderby=count&order=desc&per_page=15&page=$page'));
dynamic json = jsonDecode(response.body);
(json as List).forEach((v) {
categories.add(PostCategory.fromJson(v));
});
} catch (e) {
//TODO Handle No Internet Response
}
return categories;
}
}
The error is on the return posts;
Exception has occurred.
_TypeError (type 'List<dynamic>' is not a subtype of type 'FutureOr<List<PostEntity>>')
change var posts = [] to List<PostEntity> posts = []
static Future<List<PostEntity>> getPostsList(
{int category = 0, int page = 1}) async {
List<PostEntity> posts = []; //<-- change var posts = [] to List<PostEntity> posts = []
try { ...

Invalid argument(s): Illegal argument in isolate message : (object is a closure - Function 'createDataList':.)

I tried to fetch data from the internet with moviedb API, I followed the tutorial at https://flutter.io/cookbook/networking/fetch-data/
but I'm getting the below error.
Invalid argument(s): Illegal argument in isolate message : (object is a closure - Function 'createDataList':.)
This my code
Future<List<DataModel>> fetchData() async{
final response = await http.get("https://api.themoviedb.org/3/movie/now_playing?api_key=d81172160acd9daaf6e477f2b306e423&language=en-US");
if(response.statusCode == 200){
return compute(createDataList,response.body.toString());
}
}
List<DataModel> createDataList(String responFroJson) {
final parse = json.decode(responFroJson).cast<Map<String, dynamic>>();
return parse.map<DataModel> ((json) => DataModel.fromtJson(json)).toList();
}
Screenshot of the error message
compute can only take a top-level function, but not instance or static methods.
Top-level functions are functions declared not inside a class
and not inside another function
List<DataModel> createDataList(String responFroJson) {
...
}
class SomeClass { ... }
should fix it.
https://docs.flutter.io/flutter/foundation/compute.html
R is the type of the value returned. The callback argument must be a top-level function, not a closure or an instance or static method of a class.
As per today (2020. Aug) the compute is working fine with static methods.
For me, the issue was that I was trying to return a http.Response object from the compute() methods.
What I did is I've created a simplified version of this class, containing what I need:
class SimpleHttpResponse {
String body;
int statusCode;
Map<String, String> headers;
}
Then I've updated the original method from this:
static Future<http.Response> _executePostRequest(EsBridge bridge) async {
return await http.post(Settings.bridgeUrl, body: bridge.toEncryptedMessage());
}
to this:
static Future<SimpleHttpResponse> _executePostRequest(EsBridge bridge) async {
http.Response result = await http.post(Settings.bridgeUrl, body: bridge.toEncryptedMessage());
if (result == null) {
return null;
}
SimpleHttpResponse shr = new SimpleHttpResponse();
shr.body = result.body;
shr.headers = result.headers;
shr.statusCode = result.statusCode;
return shr;
}
Worked like charm after this change. Hope this helps somebody ranning into similar problem.

How can I make sure a method waits for a http response instead of returning null in Dart?

I am trying to write a little command line library in Dart to work with the Facebook API. I have a class 'fbuser' which gets the auth-token and user-id as properties and has a method 'groupIds' which should return a List with all IDs of groups from the user.
When I call the method it returns null altough the two possbile returns are called after the http response. What did I do wrong?
Here is my code:
import 'dart:convert'; //used to convert json
import 'package:http/http.dart' as http; //for the http requests
//config
final fbBaseUri = "https://graph.facebook.com/v2.1";
final fbAppID = "XXX";
final fbAppSecret = "XXY";
class fbuser {
int fbid;
String accessToken;
fbuser(this.fbid, this.accessToken); //constructor for the object
groupIds(){ //method to get a list of group IDs
var url = "$fbBaseUri/me/groups?access_token=$accessToken"; //URL for the API request
http.get(url).then((response) {
//once the response is here either process it or return an error
print ('response received');
if (response.statusCode == 200) {
var json = JSON.decode(response.body);
List groups=[];
for (int i = 0; i<json['data'].length; i++) {
groups.add(json['data'][i]['id']);
}
print(groups.length.toString()+ " Gruppen gefunden");
return groups; //return the list of IDs
} else {
print("Response status: ${response.statusCode}");
return (['error']); //return a list with an error element
}
});
}
}
void main() {
var usr = new fbuser(123, 'XYY'); //construct user
print(usr.groupIds()); //call method to get the IDs
}
At the moment the output is:
Observatory listening on http://127.0.0.1:56918
null
response received
174 Gruppen gefunden
The method runs the http request but it returns null immediately.
(I started programming this summer. Thanks for your help.)
return http.get(url) // add return
void main() {
var usr = new fbuser(123, 'XYY'); //construct user
usr.groupIds().then((x) => print(x)); //call method to get the IDs
// or
usr.groupIds().then(print); //call method to get the IDs
}