flutterdartflutter-layoutflutter-dependenciesdart-http

#I/flutter (11573): Text("Error Occured: FormatException: Invalid radix-10 number (at character 1)\n//api.ergagro.com:112\n^\n")


Hello I tried to make a GET HTTP REQUEST with two parameters to an endpoint but I kept on getting this error I have tried different approaches but it still not working. Please somebody help I am new to flutter this is the error message - #I/flutter (11573): Text("Error Occured: FormatException: Invalid radix-10 number (at character 1)\n//api.ergagro.com:112\n^\n")

Service function

 Future<CheckInventoryStatus> checkDailyStatus() async {
    
    final queryParameters = {
      'userId': 'b6caf34c-a425-4710-a3ee-aa22a382882a',
      'agentId': '57',
    };

    final url = new Uri.http('http://api.ergagro.com:112',
          '/CheckDailyStockTakingStatus', queryParameters);

    final response = await http.get(url);

    if (response.statusCode == 200) {
      final jsonStatus = jsonDecode(response.body);
      return CheckInventoryStatus.fromJson(jsonStatus);
    } else {
      throw Exception();
    }
  }


Future builder:

 FutureBuilder<CheckInventoryStatus>(
              future: checkDailyStatus(),
              builder: (context, snapshot) {
                if (snapshot.hasData) {
                  final status = snapshot.data;
                  return Text("Name: ${status.message}");
                } else{
                  return Text(snapshot.error.toString());
                }
                
              },
            ),

Model:

// To parse this JSON data, do
//
//     final checkInventoryStatus = checkInventoryStatusFromJson(jsonString);

import 'dart:convert';

CheckInventoryStatus checkInventoryStatusFromJson(String str) => CheckInventoryStatus.fromJson(json.decode(str));

String checkInventoryStatusToJson(CheckInventoryStatus data) => json.encode(data.toJson());

class CheckInventoryStatus {
    CheckInventoryStatus({
        this.statusCode,
        this.message,
        this.success,
        this.stock,
    });

    int statusCode;
    String message;
    bool success;
    Stock stock;

    factory CheckInventoryStatus.fromJson(Map<String, dynamic> json) => CheckInventoryStatus(
        statusCode: json["StatusCode"],
        message: json["Message"],
        success: json["Success"],
        stock: Stock.fromJson(json["Stock"]),
    );

    Map<String, dynamic> toJson() => {
        "StatusCode": statusCode,
        "Message": message,
        "Success": success,
        "Stock": stock.toJson(),
    };
}

class Stock {
    Stock({
        this.oid,
        this.createdBy,
        this.createdOn,
        this.modifiedBy,
        this.modifiedOn,
        this.season,
        this.seasonTitle,
        this.anchor,
        this.anchorName,
        this.anchorAcronym,
        this.distributionCentre,
        this.distributionCentreName,
        this.agent,
        this.stateCoordinator,
        this.stockDate,
        this.transId,
        this.stateCoordinatorId,
        this.agentId,
        this.userId,
        this.stockItems,
    });

    int oid;
    String createdBy;
    DateTime createdOn;
    dynamic modifiedBy;
    dynamic modifiedOn;
    dynamic season;
    dynamic seasonTitle;
    int anchor;
    dynamic anchorName;
    dynamic anchorAcronym;
    int distributionCentre;
    dynamic distributionCentreName;
    String agent;
    String stateCoordinator;
    DateTime stockDate;
    String transId;
    String stateCoordinatorId;
    String agentId;
    String userId;
    List<dynamic> stockItems;

    factory Stock.fromJson(Map<String, dynamic> json) => Stock(
        oid: json["Oid"],
        createdBy: json["CreatedBy"],
        createdOn: DateTime.parse(json["CreatedOn"]),
        modifiedBy: json["ModifiedBy"],
        modifiedOn: json["ModifiedOn"],
        season: json["Season"],
        seasonTitle: json["SeasonTitle"],
        anchor: json["Anchor"],
        anchorName: json["AnchorName"],
        anchorAcronym: json["AnchorAcronym"],
        distributionCentre: json["DistributionCentre"],
        distributionCentreName: json["DistributionCentreName"],
        agent: json["Agent"],
        stateCoordinator: json["StateCoordinator"],
        stockDate: DateTime.parse(json["StockDate"]),
        transId: json["TransId"],
        stateCoordinatorId: json["StateCoordinatorId"],
        agentId: json["AgentId"],
        userId: json["UserId"],
        stockItems: List<dynamic>.from(json["StockItems"].map((x) => x)),
    );

    Map<String, dynamic> toJson() => {
        "Oid": oid,
        "CreatedBy": createdBy,
        "CreatedOn": createdOn.toIso8601String(),
        "ModifiedBy": modifiedBy,
        "ModifiedOn": modifiedOn,
        "Season": season,
        "SeasonTitle": seasonTitle,
        "Anchor": anchor,
        "AnchorName": anchorName,
        "AnchorAcronym": anchorAcronym,
        "DistributionCentre": distributionCentre,
        "DistributionCentreName": distributionCentreName,
        "Agent": agent,
        "StateCoordinator": stateCoordinator,
        "StockDate": stockDate.toIso8601String(),
        "TransId": transId,
        "StateCoordinatorId": stateCoordinatorId,
        "AgentId": agentId,
        "UserId": userId,
        "StockItems": List<dynamic>.from(stockItems.map((x) => x)),
    };
}


Solution

  • use

            final url = new Uri.http('api.ergagro.com:112',
                '/CheckDailyStockTakingStatus', queryParameters);
    

    instance of

    final url = new Uri.http('http://api.ergagro.com:112',
          '/CheckDailyStockTakingStatus', queryParameters);
    

    just remove http:// from url
    when you use Uri.http
    it's wok for me thanks.