为小部件调用两个不同端点的最佳实践?

时间:2018-05-15 03:02:06

标签: firebase dart flutter

为了呈现某些内容,需要两个不同的端点时,最佳做法是什么?现在我正在筑巢FutureBuilders,但这看起来并不理想。

示例:

@override
Widget build(BuildContext context) {
  return new FutureBuilder(
    future: Firestore.instance.collection('games').document(this.documentId).get(),
    builder: (BuildContext gamesContext, AsyncSnapshot gamesSnapshot) {
      return new FutureBuilder(
        future: Firestore.instance.collection('profiles').document(gamesSnapshot.data['authorId']).get(),
        builder: (BuildContext profilesContext, AsyncSnapshot profilesSnapshot) {
          // Code goes here.
        }
      ),
    }
  ),
}

必须采用更清洁的方式,对吧?

1 个答案:

答案 0 :(得分:2)

您可以使用一个FutureBuilder

执行此操作
Future<AsyncSnapshot> getProfiles() async {
  var gamesSnapshot = await Firestore.instance.collection('games').document(this.documentId).get();
  return Firestore.instance.collection('profiles').document(gamesSnapshot.data['authorId']).get()
}

@override
Widget build(BuildContext context) {
  return new FutureBuilder(
    future: getProfiles(),
    builder: (BuildContext profilesContext, AsyncSnapshot profilesSnapshot) {
      // Code goes here.
    }
  ),
}