Flutter Shared Preferences Auth FIle

问题描述 投票:0回答:3

我正在尝试编写一个auth文件,其中包含一个包含共享首选项值的决赛列表。我可以在我的其他文件中导入该auth文件,我可以得到名称或电子邮件,而无需在每个文件中导入共享首选项。

它可能看起来更平滑,更清洁。

我认为这样的事情会奏效,但事实并非如此

/// ------------Auth------------ ///
final email = getEmail();

getEmail() async {
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  return prefs.getString('email');
}

有人知道怎么做吗?

问候,女孩

dart flutter
3个回答
0
投票

我假设你想在多个文件中使用该方法。您的代码的问题是getEmail方法标记为async,这意味着它必须返回Future。想象一下,当你将方法标记为async时,它意味着它将在不久的将来返回一些东西(或完成执行)。什么时候 ?好吧你不确切知道什么时候,所以当方法“完成”时你需要得到“通知”,这就是为什么你要使用Future。像这样的东西:

Future<String> getEmail() async {
  final SharedPreferences prefs = await SharedPreferences.getInstance();
  return prefs.getString('email');
}

class ThisIsTheClassWhereYouWantToUseTheFunction {
   //let's say you have a method in your class called like the one below (it can be any other name)
  void _iNeedTheEmailToPrintIt() {
     //this is the way you can call the method in your classes, this class is just an example.
     getEmail().then((thisIsTheResult){ // here you "register" to get "notifications" when the getEmail method is done.
        print("This is the email $thisIsTheResult");
     });
  }
}

0
投票

你可以定义一个类Auth或更好的scoped_model

这是一个类实现

class Auth {
   get email() {
       final SharedPreferences prefs = await SharedPreferences.getInstance();
       return prefs.getString('email');  
   }

   set email(String em) {
       final SharedPreferences prefs = await SharedPreferences.getInstance();
       pref.setString('email', em);
   }

}

and now you can call it in your widgets :)



0
投票

试试这个;

  1. 制作dart文件(文件名和类名称ShareUtils)

添加以下代码

import 'package:shared_preferences/shared_preferences.dart';
import 'dart:async';

class ShareUtils {
  static ShareUtils _instance;
  SharedPreferences ShareSave;

  factory ShareUtils() => _instance ?? new ShareUtils._();

  ShareUtils._();

  void Instatce() async {
    ShareSave = await SharedPreferences.getInstance();
  }

  Future<bool> set(key, value) async {
    return ShareSave.setString(key, value);
  }

  Future<String> get(key) async {
    return ShareSave.getString(key);
  }
}

2.添加main.dart

class MyApp extends StatelessWidget {
  static ShareUtils shareUtils;

  @override
  Widget build(BuildContext context) {
    ThemeData mainTheme = new ThemeData(
    );  
    shareUtils = new ShareUtils();
    shareUtils.Instatce();
    MaterialApp mainApp = new MaterialApp(
      title: "Your app",
      theme: mainTheme,
      home: new SplashPage(),
      debugShowCheckedModeBanner: true,
      routes: <String, WidgetBuilder>{
       "RegisterPage": (BuildContext context) => new RegisterPage(),
       "HomePage": (BuildContext context) => new HomePage(),         
     },
   );
    return mainApp;
  }
}

3.设置

void UserInfo(code, token) async{
    await MyApp.shareUtils.set("token", token);
    await MyApp.shareUtils.set("code", code);
    await Navigator.of(context).pushNamed("HomePage");
}

4.GET

Future NextPage() async {
MyApp.shareUtils.get("token").then((token) {
  print(token);
  if (token == null || token == "") {
    Navigator.of(context).popAndPushNamed("RegisterPage");
  } else {
    Navigator.of(context).popAndPushNamed("HomePage");
  }
});

}

希望能有所帮助。

© www.soinside.com 2019 - 2024. All rights reserved.