【发布时间】:2021-11-20 07:03:29
【问题描述】:
我正在尝试根据存储在共享首选项中的数据显示文本文件中的数据我有另一个屏幕可以将数据保存在文本文件中我之前有一个流构建器它是未来的构建器所以我正在尝试刷新屏幕当从第二个屏幕回来时,我试图在弹出时调用一个方法,该方法在提供者的视图模型类中被调用,但流构建器没有得到更新 这是代码
获取数据
Future<List<String>> fetchdata() async {
SharedPreferences prefs = await SharedPreferences.getInstance();
String? category = prefs.getString('category');
if (category != null) {
lines = await locator<JsonAPI>().fetchquotes(category);
} else {
lines = await locator<JsonAPI>().fetchquotes('quotes');
}
// data = lines as Future<List<String>>;
notifyListeners();
return lines;
}
流生成器
var quotesdata = Provider.of<HomeViewModel>(context, listen: false);
StreamBuilder(
stream: quotesdata.fetchdata().asStream(),
builder: (context, AsyncSnapshot snapshot) {
if (snapshot.hasData) {
List<String> lines = quotesdata.lines;
// List<String>? lines = snapshot.data as List<String>?;
return ScreenShotWidget(
homeViewModel: quotesdata,
list: lines,
);
} else {
return Container();
}
}),
弹出时我调用的方法
function(data) {
category = data.toString();
fetchdata();
notifyListeners();
setState() {}
}
知道如何更新屏幕
【问题讨论】:
-
你能指定quotesdata是什么以及它是在哪里创建的吗?
-
quotesdata 是视图模型类的对象
-
@Lulupointu 更新了问题
标签: android ios flutter flutter-layout stream-builder