3
votes

In Flutter Web, when I run a debug session in Chrome from IDE, all the data (cookies, local storage, etc) is wiped out on the next run. For example, if I log in on my website, I become logged out on the next run.

How to keep browser data between runs?

PS: I had read a similar question: Flutter web local storage but its author satisfied with the explanation of why the data is not kept. I want to keep the data and curious about a way to achieve that. On an Android device (even on an Emulator), data is kept. I want to achieve the same in the browser.

1

1 Answers

5
votes

You can use the --web-port option to always use the same port when you start. This will ensure that the localstorage is retained between multiple runs of debug session.

You can run the following app example as shown below and always use the same port between different runs.

flutter run -d chrome --web-port=8080

import 'package:flutter/material.dart';
import 'dart:html';

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

class LocalStorageDemo extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    String message;
    if (window.localStorage.containsKey('my-key')) {
      message = 'from storage ${window.localStorage['my-key']}';
    } else {
      window.localStorage['my-key'] = "Hello World";
      message = 'New added Message ${window.localStorage['my-key']}';
    }
    return MaterialApp(
      home: Container(
        child: Center(
          child: Text(message),
        ),
      ),
    );
  }
}

Android Studio

In android studio you can configure this in the run configurations. enter image description here

enter image description here