[Solved] The name ’Response’ is defined in the libraries

In this example, we are going to show you how to solve "The name 'Response' is defined in the libraries 'package:dio/src/response.dart" or "Error: 'Response' is imported from both dio and get the package" error. This is the conflict between Dio and Get packages for Response, FormData, and MultipartFile part.

The name 'Response' is defined in the libraries 'package:dio/src/response.dart 
(via package:dio/dio.dart)'  and 'package:get/get_connect/http/src/response/response.dart'.
Try using 'as prefix' for one of the import directives, or hiding the name 
from all but one of the imports.dart (ambiguous_import)

OR:

Error: 'Response' is imported from both 'package:dio/src/response.dart' 
and 'package:get/get_connect/http/src/response/response.dart'. import 'package:get/get.dart';

This is the conflict between Dio and GetX packages, in where Response, FormData, and MultipartFile declarations are used in both packages.

To solve this error, you have to hide Response, FormData, and MultipartFile from import on one of them. First, decide, which package are you going to use for the REST API request. 

If you are using Dio as the primary package for Request, hide these parts of the package from GetX package.

import 'package:get/get.dart' hide Response, FormData, MultipartFile;

If you are using Get as the primary package for Request, hide these parts of the package from the Dio package.

import 'package:dio/dio.dart' hide Response, FormData, MultipartFile;

import 'package:dio/dio.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart' hide Response, FormData, MultipartFile;

void main(){
  runApp(MyApp());
}

class MyApp extends StatelessWidget{
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      home: Home(),
    );
  }
}

class Home extends StatefulWidget{
  @override
  _HomeState createState() => _HomeState();
}

class _HomeState extends State<Home> { 

  getData() async {
     var dio = Dio();
     Response res = await dio.get("https://www.fluttercampus.com/json.php", 
         options: Options( 
            responseType: ResponseType.plain,
         ));

     if(res.statusCode == 200){

        print("Connection successful");
        print(res.data);
        //encoded response:
        //{error: false, msg: Hello World, data: [Hari, Krishna, John]}

        //Non encoded plain response:
        //{"error":false,"msg":"Hello World","data":["Hari","Krishna","John"]}
     }else{
       print("Error while connecting to server.");
     }
  }

  @override
  Widget build(BuildContext context) { 
    return  Scaffold(
          appBar: AppBar(
            title: Text("REST API"),
            backgroundColor: Colors.redAccent
          ),
          body: Container(
            padding: EdgeInsets.only(top:20, left:20, right:20),
            alignment: Alignment.topCenter,
            child: Column(
              children: [

                 ElevatedButton(
                  onPressed: () async {
                     getData();
                  }, 
                  child: Text("Get Data")
                ),
                
            ],)
          )
       );
  }
}

In this way, you can solve the conflict between Dio and Get packages for HTTP requests.

No any Comments on this Article


Please Wait...