getter'uid'被调用为null-但只有一半的时间

时间:2020-11-05 04:14:15

标签: firebase flutter dart firebase-authentication

因此,我的Flutter应用程序中有一个屏幕,该屏幕应该显示特定用户的所有注释。我对Firestore进行了结构设计,以便收集注释,每个用户都有一个名为uid的文档。然后,他们的所有注释都存储在其文档下的集合(用户注释)中。

我在这里遇到的问题是,当您尝试访问应用程序中的便笺页面时,您会收到错误消息

getter'uid'在null上被调用。 接收者:null 尝试调用:uid

但是,当我简单地从Flutter应用程序中单击运行时,一切正常。您可以在屏幕上看到所有注释。这是我的笔记屏幕。

import 'package:flutter/material.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'welcome_screen.dart';

class NoteScreen extends StatefulWidget {
  static const String id = 'note_screen';

  @override
  _NoteScreenState createState() => _NoteScreenState();
}

class _NoteScreenState extends State<NoteScreen> {
  final _auth = FirebaseAuth.instance;
  User loggedInUser;

  @override
  void initState() {
    super.initState();
    getCurrentUser();
  }

  void getCurrentUser() async {
    try {
      final user = await _auth.currentUser;
      if (user != null) {
        loggedInUser = user;
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text('Field Notes'),
        actions: <Widget>[
          IconButton(
            icon: const Icon(Icons.chat),
            tooltip: 'Messages',
            onPressed: () {},
          ),
          IconButton(
            icon: const Icon(Icons.exit_to_app),
            tooltip: 'Log Out',
            onPressed: () {
              _auth.signOut();
              Navigator.pushNamed(context, WelcomeScreen.id);
            },
          ),
        ],
      ),
      body: StreamBuilder(
        stream: FirebaseFirestore.instance
            .collection('notes').doc(loggedInUser.uid).collection('usernotes')
            .snapshots(),
        builder: (ctx, streamSnapShot) {
          if(!streamSnapShot.hasData) return const Text('Loading...');
          if (streamSnapShot.connectionState == ConnectionState.waiting) {
            return Center(
              child: CircularProgressIndicator(),
            );
          }
          final noteData = streamSnapShot.data.docs;
          return ListView.builder(
            itemCount: noteData.length,
            itemBuilder: (ctx, index) => Container(
              padding: EdgeInsets.all(8),
              child: Text(noteData[index]['text']),
            ),
          );
        },
      ),
      floatingActionButton:
          FloatingActionButton(child: Icon(Icons.add), onPressed: () {
            FirebaseFirestore.instance.collection('notes').doc(loggedInUser.uid).collection('usernotes').add({
              'text' : 'This was added by clicking the button!'
            });
          }),
    );
  }
}

1 个答案:

答案 0 :(得分:1)

currentUser的类型为User,因此您无需使用await,因为它不会返回Future。您可以执行以下操作:

FirebaseFirestore.instance.collection('notes').doc(_auth.currentUser.uid).collection('usernotes').snapshots(),

并且:

FloatingActionButton(child: Icon(Icons.add), onPressed: () {
  FirebaseFirestore.instance.collection('notes').doc(_auth.currentUser.uid).collection('usernotes').add({'text' : 'This was added by clicking the button!'});
});