Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/flutter/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Flutter 颤振共享_首选项不存储数据_Flutter_Flutter Sharedpreference - Fatal编程技术网

Flutter 颤振共享_首选项不存储数据

Flutter 颤振共享_首选项不存储数据,flutter,flutter-sharedpreference,Flutter,Flutter Sharedpreference,我正在尝试在我的Flatter应用程序中实现包共享的_首选项 我已将该包包含在pubspec.yaml文件中 我创建了一个名为StorageUtil的助手类来管理项目中的共享_首选项 这是共享的\u首选项\u util.dart: import 'package:shared_preferences/shared_preferences.dart'; class StorageUtil { static StorageUtil _storageUtil; static SharedPr

我正在尝试在我的Flatter应用程序中实现包共享的_首选项

我已将该包包含在pubspec.yaml文件中

我创建了一个名为StorageUtil的助手类来管理项目中的共享_首选项

这是共享的\u首选项\u util.dart:

import 'package:shared_preferences/shared_preferences.dart';

class StorageUtil {
  static StorageUtil _storageUtil;
  static SharedPreferences _preferences;

  static Future<StorageUtil> getInstance() async {
    if (_storageUtil == null) {
      // keep local instance till it is fully initialized.
      var secureStorage = StorageUtil._();
      await secureStorage._init();
      _storageUtil = secureStorage;
    }
    return _storageUtil;
  }

  StorageUtil._();
  Future _init() async {
    _preferences = await SharedPreferences.getInstance();
  }

  // get string
  static String getString(String key, {String defValue = ''}) {
    if (_preferences == null) return defValue;
    return _preferences.getString(key) ?? defValue;
  }

  // put string
  static Future<bool> putString(String key, String value) {
    if (_preferences == null) return null;
    return _preferences.setString(key, value);
  }
}
打印输出:

I/flutter (29912): token despues ed shared:fy0Pk1asTOejYrWYXbULBu:APA91bGk2TLqdiLbGvZp7IrTggLfjwYcvhENB83RQj-7x1CEPoou_cY7Rq0eB6rFqEfb386pxSsWAogRc8HwQjK0Y9q9kyKgzSJ6ZCQ3qhFBIjaLhS2sZKJ-N1k7hdrRHdboyUb3WvGS
稍后,我将尝试获取共享的_首选项,如下所示:

final _tokenFb = StorageUtil.getString("token_firebase");
但它始终是空字符串

print("Token firebase en authbloc: " + _tokenFb);
输出:

I/flutter (29912): Token firebase en authbloc: 

我缺少什么?

\u首选项
为空,因为您没有在类上调用
wait-StorageUtil.getInstance()
。因此,不会调用
\u init
方法,也不会初始化
SharedReferences

你应该这样称呼它:

\u刷新令牌(){
_firebaseMessaging.getToken().then((令牌)异步{
打印('token:$token');
var-storageUtil=await-storageUtil.getInstance();
storageUtil.putString(“token_firebase”,token.toString());
打印('token dispues de shared:'+token);
},一个错误:_(故障);
}
当然,
StorageUtil.getString
也是如此

I/flutter (29912): Token firebase en authbloc: