skip to Main Content

I am developing a flutter web app in Anroid Studio.
When testing the app and run it from Android Studio, I would like to it to run in Chrome using my current user profile, as this user profile has all necessary passwords stored.

Whatever I try, when runnung the app from Android Studio, Anroid Studio would start a new Chrome instance with an empty Profile. This makes sense, as Android Studio starts a new virtual device which don’t have any information on user profiles stored.
This makes my developement-test cycles slow because I need to login with my google account credentials each time.

I learned the Anroid Studio Flutter plugin starts Flutter applications using the flutter run command from the Flutter SDK.
What I tried so far, is change the Settings in Anroid Studio: Settings > Tools > Web Browsers > Chrome
both to use a custom user data directory:
"%LOCALAPPDATA%GoogleChromeUser Data" which I get from typing chrome://version/ in my current chrome browser.
and/or to use command line options:
--profile-directory=Default

This didn’t help, and also the JetBrain Support says,
Thanks in advance for your help.

3

Answers


  1. You can run the app using flutter run -d web-server. It will start a local web server and provide you with a URL that you can open in any browser.

    $ flutter run -d web-server
    
    Launching lib/main.dart on Web Server in debug mode...
    Waiting for connection from debug service on Web Server...         21.3s
    lib/main.dart is being served at http://localhost:56153
    The web-server device requires the Dart Debug Chrome extension for debugging. Consider using the Chrome or Edge devices for an improved development
    workflow.
    
    🔥  To hot restart changes while running, press "r" or "R".
    For a more detailed help message, press "h". To quit, press "q".
    
    Login or Signup to reply.
  2. First of all not the Android Studio is responsible for launching Chrome but a flutter tool.

    This tool creates every time a new Profile into a temporary directory as a result all histories/cookies/passwords/etc. are lost whenever a new debugsession is started.

    Here we can mess around with Chrome flags, unfortunately I couldn’t connect it to internal Chrome profiles (remote debugger couldn’t attach to the newly opened Chrome process) however I could connect it to a minimalist Profile which flutter tool uses as a skeleton when it is creates a new Profile.

    The trick is to copy this minimalist Profile to a new path (if possible avoid pathes which contains spaces) so it can be reused as many times as you want and shared between multiple flutter web projects.

    1. Go to PROJECT_ROOT.dart_toolchrome-device folder if .dart_tool
      and/or chrome-device folder not exists then run you flutter
      project with Chrome (web) target so .dart_toolchrome-device
      will be generated

    2. Inside PROJECT_ROOT.dart_toolchrome-device there should be a folder called Default. Create a new Profile container on your system (mine is at: D:flutter_chrome_profiles) and copy the Default folder (including the Default folder not just it’s content) to the newly created Profile container

    This part of answer is based on this answer.

    1. Close Android Studio

    2. Go to flutterbincache and remove a file named: flutter_tools.stamp

    3. Go to flutterpackagesflutter_toolslibsrcweb and open the file chrome.dart

    4. Change this '--user-data-dir=${userDataDir.path}', to your new Profile container’s path (mine is: '--user-data-dir=D:\flutter_chrome_profiles',)

    Now every time whenever you lauch any flutter web project your histories/cookies/passwords/etc. will be remained.

    If you have a lot of passwords then you should export them and import them into this newly created Profile, you can easily do this if this new Profile is opened by Android Studio.

    Login or Signup to reply.
  3. I also needed to keep my Google authentication everytime I wanted to debug the app.
    The way I did it was to set up persistency by doing the following:

        _auth = firebase_auth.FirebaseAuth.instanceFor(
          app: firebaseApp,
          persistence: firebase_auth.Persistence.INDEXED_DB,
        );
    

    I also had to make this change using authStateChanges(), as for some reason firebase user came as null:

      static Future<User> getFirebaseUser() async {
        //return await auth().currentUser;
        return _auth.authStateChanges().first;
      }
    

    This was recently fixed by FlutterFire, so make sure all the dependencies are updated. I also removed all the scripts from index.html that were previously required.

    So, this solves the "lose auth after refresh" problem. Then, every time you close and open Chrome (ie everytime you start debugging from scratch) a fresh new log-in was required. To solve this, you can force the code to run on the same web-port everytime, so it keeps local data in between sessions. Don’t forget the -d chrome as it wasn’t working previously

    flutter run -d chrome --web-port 7357

    Since I didn’t want to run it from command, I’ve added a new Run/Debug configuration like in the image below.

    enter image description here

    Good luck!

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search