karthik
karthik

Reputation: 181

How to fix the proxy setup issue with flutter?

I am new to flutter. We have a proxy setup in the network. The proxy is already applied to android studio and it is working fine. While creating a new application it is working fine. But, get packages is not working. It returns

Could not resolve URL "https://pub.dartlang.org".

pub get failed (69) -- attempting retry 1 in 1 second...'

set https_proxy=USERNAME:PASSWORD@hostname:port' I tried this code. Even though it is not working

Get packages is not working

Upvotes: 7

Views: 18469

Answers (3)

mostafa3dmax
mostafa3dmax

Reputation: 1017

In the flutterSDK>bin folder edit flutter.bat file and add this line before last line:

SET HTTPS_PROXY=127.0.0.1:1080

and save. After that you can also use android studio pub get button instead terminal or CMD

If you add HTTP_PROXY in this file you getting error like this : WebSocketException: Invalid WebSocket upgrade request when running the app. In mosy case you need just HTTPS_PROXY .

Also in Windows OS for getting proxy from system proxy you can add this code to get proxy from windows proxy and set it automatically :

reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyEnable | find /i "0x1" >NUL && goto :setProxy
goto :end
:setProxy
FOR /F "tokens=*" %%g IN ('reg query "HKEY_CURRENT_USER\SOFTWARE\Microsoft\Windows\CurrentVersion\Internet Settings" /v ProxyServer ^|grep ProxyServer ^| awk ^'{printf $3}^'') do (SET prx=%%g)
SET HTTPS_PROXY=%prx%
:end

by the second approach you just need disable system proxy to disable flutter proxy.

Upvotes: 0

carloswm85
carloswm85

Reputation: 2382

You should be fine with setting the correct environment variables at the current user scope only (not at "machine" level). Add HTTP_PROXY, HTTPS_PROXY and NO_PROXY.

enter image description here

Since I have to switch those variables in my laptop everytime I'm outside my job, I've made these script for Powershell.

Add Variables

File name: proxy-variables-add.ps1

Write-Host "ADDING VARIABLES"

# Define your environment variables here
$EnvVariable1 = "http://10.1.33.254:80"
$EnvVariable2 = "http://10.1.33.254:80"
$EnvVariable3 = "localhost,127.0.0.1,::1,LOCALHOST"

# Set environment variables for the current user
[Environment]::SetEnvironmentVariable("HTTP_PROXY", $EnvVariable1, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", $EnvVariable2, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("NO_PROXY", $EnvVariable3, [System.EnvironmentVariableTarget]::User)

# Display a message to confirm that the variables have been set
Write-Host "Environment USER variables have been set:"
Write-Host "HTTP_PROXY = $EnvVariable1"
Write-Host "HTTPS_PROXY = $EnvVariable2"
Write-Host "NO_PROXY = $EnvVariable3"

# Notify the user to restart applications to apply the changes
Write-Host "Please restart any applications that need to use the new environment variables."

Remove Variables

File name: proxy-variables-remove.ps1

Write-Host "REMOVING VARIABLES"

# Define your environment variables here
$EnvVariable1 = $null
$EnvVariable2 = $null
$EnvVariable3 = $null

# Set environment variables for the current user
[Environment]::SetEnvironmentVariable("HTTP_PROXY", $EnvVariable1, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", $EnvVariable2, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("NO_PROXY", $EnvVariable3, [System.EnvironmentVariableTarget]::User)

# Display a message to confirm that the variables have been set
Write-Host "Environment USER variables have been set:"
Write-Host "HTTP_PROXY = null (and variable deleted)"
Write-Host "HTTPS_PROXY = null (and variable deleted)"
Write-Host "NO_PROXY = null (and variable deleted)"

# Notify the user to restart applications to apply the changes
Write-Host "Please restart any applications that need to use the new environment variables."

From bash, you can run those scripts with the following code, at .bashrc:


# Colors
NC='\033[0m' # No Color
YL='\033[0;33m' # Yellow

function proxyshell {
    currentFolder=$(pwd)
    cd "$HOME/my-scripts/powershellFolder" || exit
    
    if [ "$1" == "on" ]; then
        printf "${YL}>> Running Script\n${NC}"
        echo ""
        powershell -File proxy-variables-add.ps1
        cd "$currentFolder"  || exit
        return 1
    fi

        if [ "$1" == "off" ]; then
        printf "${YL}>> Running Script\n${NC}"
        echo ""
        powershell -File proxy-variables-remove.ps1
        cd "$currentFolder"  || exit
        return 1
    fi
}

In the command line, for setting up the environment variables:

$ proxyshell on
$ proxyshell off

Result

enter image description here

Upvotes: 0

Benjamin Corben
Benjamin Corben

Reputation: 297

You need to run these 2 lines on both your Flutter folder and your project folder:

set https_proxy=USERNAME:PASSWORD@hostname:port
set http_proxy=USERNAME:PASSWORD@hostname:port

Upvotes: 9

Related Questions