首页 > 解决方案 > 如何获取用户流登录应用程序后?

问题描述

我对火力和颤振有疑问。我正在构建应用程序,登录并显示收款费用列表,名称是用户 ID。当用户登录时,他将看到包含他收藏中的项目的列表。我使用了 Stream 方法。问题是在用户登录之前,流收到空 id 并且应用程序崩溃。用户登录后有没有办法设置这个流?我会感谢任何帮助!

这是错误


The following assertion was thrown building Home(dirty):
'package:cloud_firestore/src/firestore.dart': Failed assertion: line 72 pos 12: 'path != null': is not true.


Either the assertion indicates an error in the framework itself, or we should provide substantially more information in this error message to help you determine and fix the underlying cause.
In either case, please report this assertion by filing a bug on GitHub:
  https://github.com/flutter/flutter/issues/new?template=BUG.md

The relevant error-causing widget was
    Home 
lib\screens\wrapper.dart:18
When the exception was thrown, this was the stack
#2      Firestore.collection 
package:cloud_firestore/src/firestore.dart:72
#3      DatabaseService.expenses 
package:expenses_app/services/database.dart:38
#4      Home.build 
package:expenses_app/…/home/home.dart:27
#5      StatelessElement.build 
package:flutter/…/widgets/framework.dart:4291
#6      ComponentElement.performRebuild 
package:flutter/…/widgets/framework.dart:4223


this is stream in database file
Stream<QuerySnapshot> get expenses  {
    return firestoreReference.collection(uid).snapshots();
}

this is widget, which loads after user signs in

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:expenses_app/services/auth.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:expenses_app/services/database.dart';
import 'package:provider/provider.dart';
import 'expenses_list.dart';

class Home extends StatelessWidget {
  static String currentUid;
  final AuthService _auth = AuthService();
  final FirebaseAuth firebaseAuth = FirebaseAuth.instance;


  void getUid() async {
    final FirebaseUser user = await firebaseAuth.currentUser();
    currentUid = user.uid;
    //print(currentUid);
  }


  @override
  Widget build(BuildContext context) {
    getUid();

    return StreamProvider<QuerySnapshot>.value(
      value: DatabaseService(uid: currentUid).expenses,
          child: Scaffold(
          appBar: AppBar(
            backgroundColor: Colors.red[400],
            title: Text('Expenses App'),
            elevation: 0.0,
            actions: <Widget>[
              FlatButton.icon(
                icon: Icon(Icons.person,
                color: Colors.white,),
                onPressed: () async {
                  await _auth.signOut();
                },
                label: Text('Logout',
                style: TextStyle(color: Colors.white),),

              )
            ],
          ),
          body: ExpensesList(),

      ),
    );
  }
}

这是错误

标签: firebaseflutterfirebase-realtime-databasedartfirebase-authentication

解决方案


您在 id 到达之前构建小部件 在构建返回之前检查 id 是否不为 null,如果 id 为 null 返回空容器 e


推荐阅读