【问题标题】:How to solve this error: The method '[]' was called on null. Receiver: null Tried calling: []("name")如何解决此错误:在 null 上调用了方法“[]”。接收者:null 尝试调用:[]("name")
【发布时间】:2021-02-05 09:08:46
【问题描述】:

基本上,我想在分区抽屉中显示 info admin。所以我用户StreamBuilder。然后我内联出错了

StreamBuilder<DocumentSnapshot> file:///D:/Android_project/finalyearproject/lib/sidebar/AdminDrawer.dart:26:12

这里是我的 AdminDrawer 类

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:finalyearproject/screen/login.dart';
import 'package:finalyearproject/service/auth.dart';




class AdminDrawer extends StatefulWidget {

  final String uid ;
  AdminDrawer({ this.uid });
  @override
  _AdminDrawerState createState() => _AdminDrawerState();
}

class _AdminDrawerState extends State<AdminDrawer> {



  final AuthService _authService = AuthService();

  @override

  Widget build(BuildContext context) {
    return StreamBuilder(     **//this line show the error.** 
      stream: Firestore.instance.collection('Admin').document(widget.uid).snapshots(),
        builder: (context, snapshot) {
          if (!snapshot.hasData) {
            return CircularProgressIndicator();
          } else {
            return Drawer(
              child: Column(
                children: <Widget>[
                  Container(
                    width: double.infinity,
                    padding: EdgeInsets.only(top: 40),
                    color: Colors.redAccent,
                    child: Center(
                      child: Column(
                        children: <Widget>[
                          Container(
                            padding: EdgeInsets.only(top: 40),
                            width: 100,
                            height: 100,
                          ),
                          (snapshot.data['name']),
                           Text(snapshot.data['email']),
                        ],
                      ),
                    ),
                  ),
                  SizedBox(height: 5.0),
                  ListTile(
                    leading: Icon(Icons.person_pin),
                    title: Text('Profile Admin'),
                    onTap: () async {
                        **// this area I will use the nagivator push data of info admin for updating profile. This part I will pass the data to other screen//**
                    },
                  ),
                  SizedBox(height: 5.0),
                  ListTile(
                      leading: Icon(Icons.arrow_back),
                      title: Text('Logout'),
                      onTap: () async {
                        await _authService.signOut();
                        Navigator.push(context,
                            MaterialPageRoute(
                                builder: (context) => LoginScreen()));
                      }
                  ),
                ],
              ),
            );
          }
        }
    );


  }
}

所以我需要有人帮我解决这个问题。有什么我错过的吗?

【问题讨论】:

    标签: flutter widget stream-builder


    【解决方案1】:

    快照为空,这行是问题所在:snapshot.data['name']

    人们遇到过这个问题,也许这对你有帮助:Flutter: StreamBuilder Snapshot -- No Data

    【讨论】:

      【解决方案2】:

      您的 snapshot.data 似乎为空。确保您有数据。而且我假设您的 snapshot.data['name'] 不是小部件。因此,您必须对该行发表评论。您可以在这样的小部件中使用它:

                            Container(
                              padding: EdgeInsets.only(top: 40),
                              width: 100,
                              height: 100,
                            ),
                            // Comment this line.
                            //(snapshot.data['name']),
                            Text(snapshot.data['name']),
                            Text(snapshot.data['email']),
      

      【讨论】:

        猜你喜欢
        • 2021-12-28
        • 2021-08-14
        • 1970-01-01
        • 2020-06-11
        • 1970-01-01
        • 2021-06-01
        • 1970-01-01
        • 2022-01-22
        • 1970-01-01
        相关资源
        最近更新 更多