简体   繁体   中英

How do you properly store data from a POST globally in flutter?

I'm looking to store the data from a POST globally, as I will need to use it all over the application. Although I'm having difficulty finding a proper approach. I would love to have it set upon success of the POST if possible where the data gets returned. Any help and/or advice is appreciated! Thanks all!

This is the POST function

Future<User> loginUser(String username, String password) async {
  final http.Response response = await http.post(
    'https://thisisfake.com/login',
    headers: <String, String>{
      'Content-Type': 'application/json; charset=UTF-8',
    },
    body: jsonEncode(<String, String>{
      'username': username,
      'password': password
    }),
  );
  if (response.statusCode <400) {
    return User.fromJson(json.decode(response.body));


  } else {
    throw Exception('Failed to login user');
  }
}

The best option to share data in application is to store data locally. There are loads of possibilities. The best are:

You can save data by key-value pairs or just store whole objects (in Hive there is a need to write adapter, but it is easy ).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM