Flutter throwing error when used the get method in the http package

Issue

When I am trying to get data from the internet to use it in my app via the get method provided by the flutter http package it throws this error – The argument type ‘String’ can’t be assigned to the parameter type ‘Uri’. This is my code

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



class Loading extends StatefulWidget {
  @override
  _LoadingState createState() => _LoadingState();
}

class _LoadingState extends State<Loading> {
  @override
  void getData() async {
   http.get("https://jsonplaceholder.typicode.com/todos/1")
  }

  void initState() {
    super.initState();
    getData();
  }

  Widget build(BuildContext context) {
    return Scaffold(
      body: Text("some text"),
    );
  }
}

Solution

First argument of http package request method is Uri type, So you have to change your code to this:

  void getData() async {
    final requestUrl = Uri.parse("https://jsonplaceholder.typicode.com/todos/1");
    http.get(requestUrl)
  }

Answered By – BeHappy

Answer Checked By – Mildred Charles (FlutterFixes Admin)

Leave a Reply

Your email address will not be published. Required fields are marked *