Flutter 如何使用SharedReferences在Flatter中保存字符串列表
我正试图通过使用SharedReferences保存颤振中的字符串列表。但我的问题是,我在一个.dart页面中有8个不同的类,我想在所有类上显示保存的字符串,因此SharedReferences函数必须在任何类之外编写 这是我3000行代码的一部分Flutter 如何使用SharedReferences在Flatter中保存字符串列表,flutter,dart,sharedpreferences,Flutter,Dart,Sharedpreferences,我正试图通过使用SharedReferences保存颤振中的字符串列表。但我的问题是,我在一个.dart页面中有8个不同的类,我想在所有类上显示保存的字符串,因此SharedReferences函数必须在任何类之外编写 这是我3000行代码的一部分 *`So my SharedPreferences fonction has to be outside of any classes (in this case here) and I need to be able to read it from
*`So my SharedPreferences fonction has to be outside of any classes (in this case here) and I need to be able to read it from the inside of the _MainScreenState class (in the text)`*
class MainScreen extends StatefulWidget {
@override
_MainScreenState createState() => _MainScreenState();
}
class _MainScreenState extends State<MainScreen> {
@override
Widget build(BuildContext context) {
return Container(
child: Text('one of the saved string value has to appear here');
);
}
}
*`所以我的SharedReferences函数必须在任何类之外(在这里的例子中),我需要能够从_MainScreenState类的内部(在文本中)读取它`*
类MainScreen扩展StatefulWidget{
@凌驾
_MainScreenState createState()=>\u MainScreenState();
}
类_MainScreenState扩展状态{
@凌驾
小部件构建(构建上下文){
返回容器(
child:Text('此处必须显示一个保存的字符串值');
);
}
}
在共享首选项中插入列表非常简单。请看这里:
至于访问它。SharedReference可从应用程序中的任何位置获得。因此,让他们上你的课不应该是个问题。我会尝试使用静态函数,比如:
class TestClass{
//This is available from anywhere in your project
//TestClass.getList();
static List<String> getList(){
//Get your list from preferences
//and return it.
}
}
class测试类{
//这可以从项目中的任何位置获得
//TestClass.getList();
静态列表getList(){
//从首选项获取您的列表
//然后把它还给我。
}
}
您的问题非常混乱,必须提供更多详细信息Shello,因此我基本上希望使用SharedReferences保存字符串列表。但是我想在所有的课堂上都能看到他们。