Dart 获取未来的价值<;地图<;字符串,字符串>&燃气轮机;未来建设者
我有一个未来的方法,如下所示:Dart 获取未来的价值<;地图<;字符串,字符串>&燃气轮机;未来建设者,dart,flutter,Dart,Flutter,我有一个未来的方法,如下所示: Future<Map<String,String>> readFavorites() async { SharedPreferences prefs = await SharedPreferences.getInstance(); names = prefs.getKeys(); for (var key in names) { debugPrint("key is " + key); deb
Future<Map<String,String>> readFavorites() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
names = prefs.getKeys();
for (var key in names) {
debugPrint("key is " + key);
debugPrint("value is " + prefs.get(key));
pairs.putIfAbsent(key, () => prefs.get(key));
}
return pairs;
}
Future readFavorites()异步{
SharedReferences prefs=等待SharedReferences.getInstance();
name=prefs.getKeys();
for(变量输入名称){
调试打印(“键为”+键);
debugPrint(“值为”+prefs.get(key));
pairs.putIfAbsent(key,()=>prefs.get(key));
}
返回对;
}
我想在下面的futurebuilder中获取快照长度加上地图的值:
Widget build(BuildContext ctxt) {
return Container(
child: FutureBuilder(
future: readFavorites(),
builder: (context, AsyncSnapshot<Map<String,String>> snapshot) {
if (snapshot.connectionState == ConnectionState.waiting) {
return Center(
//replace this with a loading icon
child: new CircularProgressIndicator());
} else {
return ListView.builder(
itemExtent: 90,
itemCount: snapshot.data.length, <== How to get the map length?
itemBuilder: (BuildContext context, int index) {
return SingleDish(
dish_name: snapshot.data[index],
dish_picture: snapshot.data[index]., <== How to get the value from the map?
);
});
}
},
),
);
}
Widget构建(BuildContext-ctxt){
返回容器(
孩子:未来建设者(
future:readFavorites(),
生成器:(上下文,异步快照){
if(snapshot.connectionState==connectionState.waiting){
返回中心(
//将其替换为加载图标
子对象:新循环ProgressIndicator());
}否则{
返回ListView.builder(
项目范围:90,
itemCount:snapshot.data.length,看看这段代码,它是自动解释的,您可以根据需要修改这段代码
Widget build(BuildContext ctxt) {
return Container(
child: FutureBuilder(
future: readFavorites(),
builder: (context, AsyncSnapshot<Map<String,String>> snapshot) {
switch( snapshot.connectionState){
case ConnectionState.none:
return Text("there is no connection");
case ConnectionState.active:
case ConnectionState.waiting:
return Center( child: new CircularProgressIndicator());
case ConnectionState.done:
if (snapshot.data != null){
Map<String,String> myMap = Map.from( snapshot.data ); // transform your snapshot data in map
var keysList = myMap.keys.toList(); // getting all keys of your map into a list
return ListView.builder(
itemExtent: 90,
itemCount: myMap.length, // getting map length you can use keyList.length too
itemBuilder: (BuildContext context, int index) {
return SingleDish(
dish_name: keysList[index], // key
dish_picture: myMap[ keysList[index] ], // getting your map values from current key
);
}
);
}
// here your snapshot data is null so SharedPreferences has no data...
return Text("No data was loaded from SharedPreferences");
}//end switch
},
),
);
}
Widget构建(BuildContext-ctxt){
返回容器(
孩子:未来建设者(
future:readFavorites(),
生成器:(上下文,异步快照){
交换机(快照.连接状态){
案例连接状态。无:
返回文本(“没有连接”);
案例连接状态.active:
案例连接状态。正在等待:
返回中心(子项:新循环ProgressIndicator());
案例连接状态。完成:
如果(snapshot.data!=null){
Map myMap=Map.from(snapshot.data);//在Map中转换快照数据
var keysList=myMap.keys.toList();//将映射的所有键放入一个列表中
返回ListView.builder(
项目范围:90,
itemCount:myMap.length,//获取映射长度也可以使用keyList.length
itemBuilder:(构建上下文,int索引){
返回单碟(
dish_name:keysList[index],//键
dish_picture:myMap[keysList[index]],//从当前键获取映射值
);
}
);
}
//此处快照数据为空,因此SharedReferences没有数据。。。
返回文本(“没有从SharedReferences加载数据”);
}//终端开关
},
),
);
}
snapshot.data
是一个映射
对吗?所以使用api获取数据,但我想使用快照。如何通过快照访问它?Map favorites=snapshot.data;
我如何使用映射在生成器中迭代并获取键值对?我在第一条评论中给了你一个链接,不是吗?代码运行得很好。呵呵wever,当我重新启动我的应用程序并将一个项目添加到收藏夹,然后访问收藏夹列表时,我在这一行得到了空异常:Map.from(snapshot.data);虽然SharedReferences包含数据。但我在该行遇到以下异常:对null调用了方法'forEach'。我更改了源代码。现在它更详细了。我不知道是否可以解决此问题,但此方法将帮助您解决。行映射中的“null指针异常”。from(snapshot.data);表示快照在dart中没有数据,我们可以使用可为null的运算符“?”和“?”来解决此问题,但我使用了if()块来避免此异常。如果在readFavorites方法中调用了null上的“forEach”,则行prefs.getKeys();将null返回给您的名称变量,这意味着SharedReferences没有键,问题可能是在将数据写入此变量时。
Widget build(BuildContext ctxt) {
return Container(
child: FutureBuilder(
future: readFavorites(),
builder: (context, AsyncSnapshot<Map<String,String>> snapshot) {
switch( snapshot.connectionState){
case ConnectionState.none:
return Text("there is no connection");
case ConnectionState.active:
case ConnectionState.waiting:
return Center( child: new CircularProgressIndicator());
case ConnectionState.done:
if (snapshot.data != null){
Map<String,String> myMap = Map.from( snapshot.data ); // transform your snapshot data in map
var keysList = myMap.keys.toList(); // getting all keys of your map into a list
return ListView.builder(
itemExtent: 90,
itemCount: myMap.length, // getting map length you can use keyList.length too
itemBuilder: (BuildContext context, int index) {
return SingleDish(
dish_name: keysList[index], // key
dish_picture: myMap[ keysList[index] ], // getting your map values from current key
);
}
);
}
// here your snapshot data is null so SharedPreferences has no data...
return Text("No data was loaded from SharedPreferences");
}//end switch
},
),
);
}