How to fix the proxy setup issue with flutter?
Asked Answered
C

3

7

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

Caddy answered 11/7, 2019 at 8:2 Comment(0)
P
9

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
Pippas answered 18/12, 2019 at 13:40 Comment(0)
M
0

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

Malt answered 13/9, 2023 at 17:24 Comment(0)
G
0

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

SET HTTP_PROXY=127.0.0.1:1080
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

But by setting proxy in this file you getting error like this : WebSocketException: Invalid WebSocket upgrade request when running the app. you must comment these lines after pub get finished it's work.

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 HTTP_PROXY=%prx%
SET HTTPS_PROXY=%prx%
:end

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

Gingrich answered 8/10 at 19:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.