【问题标题】:Flutter Firebase The method 'data' was called on nullFlutter Firebase 在 null 上调用了方法“数据”
【发布时间】:2021-12-18 03:03:58
【问题描述】:

我正在使用 Firestore,并尝试通过 Streambuilder 获取流。 但是,发生了这个错误。

The following NoSuchMethodError was thrown building StreamBuilder<DocumentSnapshot<Object>> 
(dirty, state: _StreamBuilderBaseState<DocumentSnapshot<Object>, 
AsyncSnapshot<DocumentSnapshot<Object>>>#32fdb):
The method 'data' was called on null.
Receiver: null
Tried calling: data()

这是我的代码。

    import 'package:cloud_firestore/cloud_firestore.dart';
    import 'package:flutter/material.dart';
    
    class UserDetailPage extends StatefulWidget {
      String uid;
      UserDetailPage(this.uid);
    
      @override
      _UserDetailPageState createState() => _UserDetailPageState();
    }
    
    class _UserDetailPageState extends State<UserDetailPage> {
      final List<String> datas = <String>['a', 'b', 'c', 'd', 'e', 'f', 'g', '1', '2','3', '4', '5', '6'];
      CollectionReference userstream = FirebaseFirestore.instance.collection('users');
    
      @override
      void initState() {
        super.initState();
    
      }
    
    
      @override
      Widget build(BuildContext context) {
        return Scaffold(
          appBar: AppBar(
            title: Text('User Detail'),
          ),
          body:_buildBody(),
        );
      }
    
     _buildBody() {
    
        return StreamBuilder(
          stream: userstream.doc(widget.uid).snapshots(),
          builder: (context, snapshot){
            Map<String, dynamic> user_data =snapshot.data.data();
            if(snapshot.hasError){
              return Text('ERROR');
            }
            if (snapshot.connectionState == ConnectionState.waiting) {
              return Center(child: CircularProgressIndicator());
            }
    
            return Padding(
              padding: const EdgeInsets.all(20.0),
              child: ListView.separated(
                padding: EdgeInsets.only(left: 20, right: 20),
                itemCount: 13,
                separatorBuilder: (BuildContext context, int index) => const Divider(),
                itemBuilder: (BuildContext context, int index){
                  return Center(
                    child: Container(
                      child: Row(
                        mainAxisAlignment: MainAxisAlignment.spaceAround,
                        children: [
                          Text(datas[index]),
                          Text(user_data[datas[index]] is int?user_data[datas[index]].toString():user_data[datas[index]])
                        ],
                      ),
                    ),
                  );
                }
              )
            );
          },
        );
      }
    }

有趣的是,这个错误发生后,我想要的结果立即出现在应用程序上。 所以我认为问题出现在 initstate() 中,但我不知道到底是什么问题。

顺便说一下,这个页面是从

UserDetailPage( doc.get('uid')!=null?doc.get('uid'):'5AJUsH5LYaQcBiTtO5MA7d6OKx72');

【问题讨论】:

    标签: firebase flutter google-cloud-firestore flutter-dependencies flutter-streambuilder


    【解决方案1】:

    AsyncSnapshot 包装异步加载的数据。在不检查数据是否可用的情况下调用snapshot.data(就像您在下面的代码中所做的那样),意味着您忽略了这一事实,最好不要使用StreamBuilder

      stream: userstream.doc(widget.uid).snapshots(),
      builder: (context, snapshot){
        Map<String, dynamic> user_data =snapshot.data.data();
    

    realtime listeners 上的 FlutterFire 文档中显示了处理流的正确方法。您需要进行的更改是您只在所有检查之后调用snapshot.data,而不是在它们之前:

      builder: (context, snapshot){
        if(snapshot.hasError){
          return Text('ERROR');
        }
        if (snapshot.connectionState == ConnectionState.waiting) {
          return Center(child: CircularProgressIndicator());
        }
        Map<String, dynamic> user_data =snapshot.data.data();
      
    

    【讨论】:

      猜你喜欢
      • 2020-08-31
      • 2021-02-16
      • 2020-11-06
      • 2019-09-23
      • 2020-06-04
      • 2021-09-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多