0

我试图在颤振中使用 StreamBuilder 来返回一个列表,但仅适用于当前登录的用户。当我尝试

StreamBuilder<QuerySnapshot>(
    stream:  _firestore.collection('meals').where('email', isEqualTo: loggedInUser.email).orderBy('date', descending: true).snapshots(),

我不断收到以下错误:

在 null 上调用了 getter 'email'。

接收方:空

尝试呼叫:电子邮件

当我取出.where零件时它工作正常,但它只返回每个用户的所有数据,我只希望它返回链接到该当前用户的数据。

任何想法都会非常有帮助,所以提前谢谢你。

杰森

编辑:这是更多上下文的更多代码:

import 'package:firebase_auth/firebase_auth.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/widgets.dart';
import 'package:intl/intl.dart';
import 'package:less_food/utilities/add_edit_meal_log.dart';
import 'package:less_food/utilities/constants.dart';

final _firestore = Firestore.instance;
FirebaseUser loggedInUser;

class MealLog extends StatefulWidget {

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

class _MealLogState extends State<MealLog> {
  final _auth = FirebaseAuth.instance;
  FirebaseUser loggedInUser;

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

    getCurrentUser();
  }

  void getCurrentUser() async {
    try {
      FirebaseUser user = await _auth.currentUser();
      if (user != null) {
        setState(() {
          loggedInUser = user;
          print('this is:  ${loggedInUser.email}');
        });
      }
    } catch (e) {
      print(e);
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: SafeArea(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.spaceBetween,
          crossAxisAlignment: CrossAxisAlignment.stretch,
          children: <Widget>[
            MealsStream(),
          ],
        ),
      ),
    );
  }
}

class MealsStream extends StatelessWidget {

  @override
  Widget build(BuildContext context) {
    return StreamBuilder<QuerySnapshot>(
    stream:  _firestore.collection('meals').where('email', isEqualTo: loggedInUser.email).orderBy('date', descending: true).snapshots(), 
      builder: (context, snapshot) {
        if (!snapshot.hasData) {
          return Center(
            child: CircularProgressIndicator(
              backgroundColor: Colors.lightBlueAccent,
            ),
          );
        }
        final meals = snapshot.data.documents.reversed;
        List<MealBubble> mealBubbles = [];
        for (var meal in meals) {
          final mealDate = meal.data['date'].toDate();
          print(DateFormat('dd-MM-yyyy').format(mealDate));
          final mealSelection = meal.data['mealSelection'];
          final mealDietChoice = meal.data['dietChoice'];

          final mealBubble = MealBubble(
            date: mealDate,
            selection: mealSelection,
            dietChoice: mealDietChoice,
          );

          mealBubbles.add(mealBubble);
        }
        return Expanded(
          child: ListView(
            reverse: false,
            padding: EdgeInsets.symmetric(vertical: 20.0),
            children: mealBubbles,
          ),
        );
      },
    );
  }
}

class MealBubble extends StatelessWidget {
  MealBubble({this.date, this.selection, this.dietChoice});

  final DateTime date;
  final String selection;
  final String dietChoice;

  @override
  Widget build(BuildContext context) {
    return Padding(
      padding: EdgeInsets.all(10.0),
      child: Container(
        color: Colors.white,
        child: Column(
          children: <Widget>[
            Material(
              borderRadius: BorderRadius.circular(10.0),
              elevation: 5.0,
              color: Colors.lightBlueAccent,
              child: Padding(
                padding: EdgeInsets.symmetric(horizontal: 5.0),
                child: Row(
                  mainAxisAlignment: MainAxisAlignment.spaceAround,
                  children: <Widget>[
                    Padding(
                      padding: const EdgeInsets.symmetric(horizontal: 13.0),
                      child: Text(
                        '${DateFormat('dd-MM-yyyy').format(date)}',
                        style: TextStyle(
                          color: Colors.white,
                          fontSize: 15.0,
                        ),
                      ),
                    ),
                    Text(
                      selection,
                      textAlign: TextAlign.start,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 15.0,
                      ),
                    ),
                    SizedBox(
                      width: 2.0,
                    ),
                    Text(
                      dietChoice,
                      textAlign: TextAlign.start,
                      style: TextStyle(
                        color: Colors.white,
                        fontSize: 15.0,
                      ),
                    ),
                    ButtonTheme(
                      materialTapTargetSize: MaterialTapTargetSize.shrinkWrap,
                      minWidth: 0,
                      child: FlatButton(
                        child: Text(
                          'Edit',
                          style: kMealLogEditButtonTextStyle,
                        ),
                        onPressed: () {
                          showModalBottomSheet(
                            context: context,
                            builder: (context) => AddEditMealLog(),
                            isScrollControlled: true,
                          );
                        },
                      ),
                    ),
                  ],
                ),
              ),
            ),
          ],
        ),
      ),
    );
  }
}


4

1 回答 1

0

问题是您的全局变量loggedInUser从未初始化。尽管如此,您确实在另一个类中初始化了另一个具有完全相同名称的变量,我认为这可能会让您感到困惑。

如您所见,您FirebaseUser使用 name 声明了两个对象loggedInUser,一个具有文件范围(它在文件中全局存在),另一个在类中,因此具有类范围(它只存在于该类中!)。当您loggedInUser在函数中初始化时,_MealLongState.getCurrentUser()您使用的是类变量,而不是全局变量。然后,当您loggedInUser从该方法访问时,MealsStream.build()您将获得尚未初始化的全局变量。

loggedInUser解决方案是从类中删除变量的声明_MealLogState

于 2020-04-02T09:26:18.967 回答