【发布时间】:2018-12-20 11:03:00
【问题描述】:
从 Firebase 检索后,Flutter setState 函数未更新列表。
我正在尝试开发 Flutter 应用。我没有更新 setState() 函数中的列表。该列表已成功从 firebase 检索。我已经在Services.dart 文件中编写了firebase 连接。
但是我的方法_getList() 没有得到main.dart 文件中的值。
main.dart
class DetailsPageState extends State<DetailsPage> {
List<Product> list;
@override
void initState() {
_checkUser(); // for getting user id from firebase auth
}
@override
Widget build(BuildContext context) {
return new Scaffold(
body: new Container(
child:new Text("data");
);
)
}
void _checkUser(){
debugPrint("Entering in _checkUser");
this.firebaseAuth.onAuthStateChanged.listen((firebaseUser)async{
_getList(firebaseUser.uid);
});
}
void _getList(String id)
debugPrint("Entering in _getList");
Services.retrieveItems(firestore, uid).then((onValue){
setState(() {
list=onValue;
debugPrint("items list:"+onValue.length.toString());
debugPrint("items list:"+listCart.length.toString());
});
});
}
}
Services.dart
static Future<List> retrieveItems(Firestore firestore, String userId) async {
List<Product> items = new List<Product>();
try {
firestore.collection("Items").document(userId)
.collection("ItemsMain").snapshots().listen((QuerySnapshot snapshot) {
List docList = snapshot.documents;
items = snapshot.documents.map((documentSnapshot) => Product.fromMap(documentSnapshot.data)).toList();
debugPrint("items:"+items.length.toString());
//return items;
});
} on Exception catch (e) {
print (e.toString());
}
debugPrint("items 2:"+items.length.toString());
return items;
}
预期结果:
输入_checkUser
进入_getList
项目:6
项目 2:6
项目清单:6
项目清单:6
实际结果:
输入_checkUser
进入_getList
项目列表:0
项目列表:0
项目 2:0
项目:6
【问题讨论】:
标签: firebase flutter google-cloud-firestore