Flutter共享首选项验证文件

时间:2019-04-05 14:55:03

标签: dart flutter

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

它看起来可能更平滑更干净。

我以为这样的事情本来可以奏效,但没有成功

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

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

有人知道怎么做吗?

问候, 简特

3 个答案:

答案 0 :(得分: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");
     });
  }
}

答案 1 :(得分: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 :)


答案 2 :(得分: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.SET

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");
  }
});

}

希望能提供帮助。