skip to Main Content

I got this error:

Unhandled Exception: FormatException: Invalid radix-10 number (at character 1)

Future<List<banner>> getBannerPictures() async
  {

    var url = Uri.http('https://woocommerce-811161-3604078.cloudwaysapps.com/wp-json/api/v1/get-home-page');
    final response = await http.get(url);
    print(response.body.toString());
    List<banner> photos = [];
    List<dynamic> data  = jsonDecode(response.body.toString());
    print(data.toString());

    for(var i =0; i<data.length; i++)
    {
      photos.add(banner.fromJson(data[i]));
    }
    return photos;

  }

2

Answers


  1. You are using the wrong Uri constructor.

    Either use Uri.parse, or use a hostname with path params instead.

    So for your code snippet the change would be:

    Future<List<banner>> getBannerPictures() async {
      var url = Uri.parse('https://woocommerce-811161-3604078.cloudwaysapps.com/wp-json/api/v1/get-home-page');
      final response = await http.get(url);
      print(response.body.toString());
      List<banner> photos = [];
      List<dynamic> data = jsonDecode(response.body.toString());
      print(data.toString());
    
      for (var i = 0; i < data.length; i++) {
        photos.add(banner.fromJson(data[i]));
      }
      return photos;
    }
    
    Login or Signup to reply.
  2. Uri.http() requires all separated params (hostname…), see the doc here: https://api.flutter.dev/flutter/dart-core/Uri/Uri.http.html

    ex:

    var uri = Uri.http('example.org', '/path', { 'q' : 'dart' });
    print(uri); // http://example.org/path?q=dart

    As suggested by Niko, with your entire url, you should use Uri.parse(), see the doc here:
    https://api.flutter.dev/flutter/dart-core/Uri/parse.html

    ex:

    final uri =
        Uri.parse('https://example.com/api/fetch?limit=10,20,30&max=100');
    print(uri); // https://example.com/api/fetch?limit=10,20,30&max=100
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search