环境:
flutter [√] Flutter (Channel stable, 2.2.3, on Microsoft Windows [Version 10.0.19042.1110], locale zh-CN)
dependencies
# 数据存储 登录信息 shared_preferences: ^2.0.6
example示例 https://pub.flutter-io.cn/packages/shared_preferences/example
import 'package:shared_preferences/shared_preferences.dart'; Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
但是直接使用就会出现错误:
MissingPluginException(No implementation found for method getAll on channel
需要在main函数增加 SharedPreferences.setMockInitialValues({}); // 处理报错
import 'package:shared_preferences/shared_preferences.dart'; void main() { SharedPreferences.setMockInitialValues({}); // 处理报错 Store<AppState> store = new Store<AppState>(mainReducer, initialState: new AppState( auth: new AuthState(), )); // Global.init().then((e) => runApp(MyApp(store: store))); // Future<SharedPreferences> _prefs = SharedPreferences.getInstance(); WidgetsFlutterBinding.ensureInitialized(); //解决加载json错误 Global.init().then((dynamic res) { print(res); if (res == 'ok') { $Api.dioUtil = DioUtil(); //实例化dio runApp(MyApp(store: store)); } }); }