运行依赖于SharedPreferences插件的测试总是会导致
MissingPluginException(No implementation found for method getAll on channel plugins.flutter.io/shared_preferences)
Run Code Online (Sandbox Code Playgroud)
我的pubspec.yaml
dev_dependencies:
flutter_test:
sdk: flutter
dependencies:
flutter:
sdk: flutter
shared_preferences: 0.2.3
Run Code Online (Sandbox Code Playgroud)
代码在应用程序本身中工作正常.我是否缺少一些我需要做的事情才能运行使用插件的测试?
问题:共享的首选项bool值正在null启动,即使prefs.getBool('myBool')返回时我已经给了它一个值null(尽管我的共享的首选项值应该已经设置并保存了)。但是,它在我按下按钮时就起作用了(我想是因为它已经完成了异步代码的运行)。
问题:如何强制共享首选项在启动时加载(因此,我的值不是null)而不必按下打印按钮?
示例代码:
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() => runApp(new MyApp());
class MyApp extends StatefulWidget {
MyApp({Key key}) : super(key: key);
@override
createState() => new MyAppState();
}
class MyAppState extends State<MyApp> {
final padding = const EdgeInsets.all(50.0);
@override
void initState() {
super.initState();
MySharedPreferences.load();
MySharedPreferences.printMyBool();
}
@override
Widget build(BuildContext context) {
return new MaterialApp(
home: new Scaffold(
body: new Padding(
padding: padding,
child: new Column(
children: <Widget>[
new Padding(
padding: …Run Code Online (Sandbox Code Playgroud) 我正在使用 flutter 构建登录屏幕,并在共享首选项中保存用户数据(如果正确)\n然后我在第二个屏幕中测试了保存的数据,它已经保存\n当应用程序关闭并再次打开时,我检查共享pref 但它变成了 null\n那是我的 saveData 和 getData 函数
\n\nString namePref,phonePref, typePref,statePref;\nFuture saveDataPref(String name,String phone, String type, String state )async{\n SharedPreferences pref = await SharedPreferences.getInstance();\n pref.setString(\'phone\', phone);\n pref.setString(\'name\', name);\n pref.setString(\'type\', type);\n pref.setString(\'state\', state);\n pref.commit();\n print(\'pref stored\');\n print(\'${pref.getString(\'phone\')}\');\n}\n\nFuture getDataPref() async{\n SharedPreferences pref = await SharedPreferences.getInstance();\n phonePref = pref.getString(\'phone\');\n namePref = pref.getString(\'name\');\n typePref = pref.getString(\'type\');\n statePref = pref.getString(\'state\');\n}\nRun Code Online (Sandbox Code Playgroud)\n\n我应该提到两件事\n“提交”函数已被弃用\n第二件事是代码正在处理已弃用的提交,但现在不是\ni删除了我所做的所有更改,但它仍然无法在我的设备上工作\和其他设备,不知道为什么!\n如果没有解决方案,是否有其他方法可以替代 flutter 中的共享首选项?\n这就是我使用 saveDataPref() 的地方
\n\nFuture login(BuildContext context) async {\n String password = passwordController.text;\n String phone = phoneController.text;\n print("entered login function");\n …Run Code Online (Sandbox Code Playgroud)