微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我如何使用提供程序使用 Future Int?

如何解决我如何使用提供程序使用 Future Int?

我正在尝试在 Appbar 中显示总文档的实时计数。我在我的控制台中得到了正确的信息,但是当我尝试通过提供者传递它时,它返回一个“未来”的实例。有人能告诉我为什么即使我等待结果并且结果在我的控制台中正确打印,我仍然得到一个 Instence 吗?

这是我获取 Future int 并将结果打印到我的控制台的地方。

class AuthenticationService extends ChangeNotifier {


 Future<int> totalJumps(jumpDict) async {
    var respectsQuery = _db.collection(jumpDict);

    var querySnapshot = await respectsQuery.get();
    var result = querySnapshot.docs.length;
    print(result);
    // notifyListeners();
    return result;
  }
}

这是它应该在 appBar 的标题中将结果显示为 int

class LazyListOnline extends StatefulWidget {
  static const String id = 'Lazy_list_online';
  @override
  _LazyListOnlinestate createState() => _LazyListOnlinestate();
}

class _LazyListOnlinestate extends State<LazyListOnline> {
  @override
  Widget build(BuildContext context) {

    String userDict = Provider.of<AuthenticationService>(context).findJumpDict;

    var _firestoreDb =
        FirebaseFirestore.instance.collection(userDict).snapshots();

    var totalJump = Provider.of<AuthenticationService>(context)
        .totalJumps(userDict)
        .toString();

    return Scaffold(
      appBar: AppBar(
        leading: IconButton(
            icon: Icon(Icons.arrow_back),onpressed: () {
              Navigator.popAndPushNamed(context,HomeDrawer.id);
            }),title: Text('totalJumps'),body: Stack(children: [
        Padding(
          padding: const EdgeInsets.all(18.0),child: Container(
             decoration: Boxdecoration(),),StreamBuilder<QuerySnapshot>(
            stream: _firestoreDb,builder: (context,snapshot) {
              if (!snapshot.hasData) return CircularProgressIndicator();
              return ListView.builder(
                  itemCount: snapshot.data.docs.length,itemBuilder: (context,int index) {
                    return JumpItem(
                      snapshot: snapshot.data,index: index,);
                  });
            }),]),);
  }
}

解决方法

我认为您只需要在 Provider 中以 int 形式存储文档数量即可。

 class DocumentData extends ChangeNotifier {
    int documentLength;

       void setCurrentLengthOfDocuments(int length) {
         this. documentLength = length;
         notifyListeners();
       }
 }

然后在 StreamBuilder 中。每次更改数据。你只需要更新。重新升级到你的例子就像这样。

StreamBuilder<QuerySnapshot>(
            stream: _firestoreDb,builder: (context,snapshot) {
              if (!snapshot.hasData) return CircularProgressIndicator();

              // Update amount of documents length
              Provider.of<DocumentData>(context,listen: false)
                .setCurrentLengthOfDocuments(lengthOfCurrentDocuments);

              return ListView.builder(
                  itemCount: snapshot.data.docs.length,itemBuilder: (context,int index) {
                    return JumpItem(
                      snapshot: snapshot.data,index: index,);
                  });
            }),]),

然后,您只需使用 Consumer 小部件即可获取此页面上每个位置的文档长度。或者直接从 Provider 获取值。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。