Hassan El dika
Hassan El dika

Reputation: 1

The argument type 'String' can't be assigned to the parameter type 'Uri'. please can anyone help me?

import 'dart:convert';

import 'package:http/http.dart' as http;

class RequestAssistant
{
  static Future<dynamic> getRequest(String url) async

  {
    http.Response response = await http.get(url);  //this is my error (url)

    if(response.statusCode == 200)
      {
        String jSonData = response.body;
        var decodeData = jsonDecode(jSonData);
        return decodeData;

      }
    else
      {
        return "Failed, No Response.";
      }
  }
}

Upvotes: 0

Views: 125

Answers (3)

Arshad
Arshad

Reputation: 37

with recent upgrades, http doesn't accept type String. Below change should work for you-

final Uri newUrl = Uri.parse(url); //add this line
http.Response response = await http.get(newUrl); 

Upvotes: 0

Abbas
Abbas

Reputation: 218

It will work for you:

http.Response response = await http.get(Uri.parse(url)); 

Upvotes: 1

KuKu
KuKu

Reputation: 7492

Just parse String to Uri.

import 'dart:convert';

import 'package:http/http.dart' as http;

class RequestAssistant
{
  static Future<dynamic> getRequest(String url) async

  {
    http.Response response = await http.get(Uri.encodeFull(url));  //this is my error (url)

    if(response.statusCode == 200)
      {
        String jSonData = response.body;
        var decodeData = jsonDecode(jSonData);
        return decodeData;

      }
    else
      {
        return "Failed, No Response.";
      }
  }
}

Upvotes: 0

Related Questions