Streambuilder不检索快照数据

时间:2020-04-02 10:09:51

标签: flutter dart stream stream-builder

StreamBuilder的快照即使通过Stream发送也没有数据。在firebase数据库中有关于某些ID的数据,但是我仍然无法检索它。为什么是空的?

我在线上看过类似的帖子,但无法解决。请帮助我迷路。

import 'dart:ui';
import 'package:flutter/material.dart';
import 'package:home_crisp/models/user.dart';
import 'package:home_crisp/services/auth.dart';
import 'package:home_crisp/services/database.dart';
import 'package:provider/provider.dart';
import 'custProfileEdit.dart';

class CustProfile extends StatefulWidget {
  @override
  _CustProfileState createState() => _CustProfileState();
}

class _CustProfileState extends State<CustProfile> {
  final AuthService _auth = AuthService();

  @override
  Widget build(BuildContext context) {
    final user = Provider.of<User>(context);
    final deviceSize = MediaQuery.of(context).size;
    return StreamBuilder<CustData>(
      stream: DatabaseService(uid: user.uid).custData,
      builder: (context, snapshot) {

        if (snapshot.data != null) {        // <------------ WHY SNAPSHOT.DATA IS EQUAL TO NULL
          CustData _custData = snapshot.data;

          return Material(
            type: MaterialType.card,
            color: Color(0xffD4EBD3),
            child: Column(
              crossAxisAlignment: CrossAxisAlignment.start,
              children: <Widget>[
                Text(_custData.username),
                Text(_custData.residence),
                Text(_custData.dateOfBirth.toString()),
                ]
          )
          );
        } else {
          return Column(
            children: <Widget>[
              Center(
                child: Text(user.uid + "\n No Data"),
              ),

              InkResponse(
                onTap: () async {
                  await _auth.signOut();
                },
                child: Container(
                  margin: EdgeInsets.only(right: deviceSize.width * 0.1),
                  child: Text(
                    "Sign out",
                  ),
                ),
              ),
            ],
          );
        }
      },
    );
  }
}

这是用户,身份验证和数据库代码

-----> "user.dart"

class User {
  final String uid;
  User({this.uid});
}

class CustData {
  final String custId;
  final String username;
  final String residence;
  final DateTime dateOfBirth;

  CustData({this.custId, this.username, this.residence, this.dateOfBirth});
}

-----> "auth.dart"

class AuthService {
 final FirebaseAuth _auth = FirebaseAuth.instance;

  // create user obj based on FirebaseUser
    User _userFormFirebaseUser(FirebaseUser user) {
    return user != null ? User(uid: user.uid) : null;
  }

  // auth change user stream
  Stream<User> get user {
    return _auth.onAuthStateChanged.map(_userFormFirebaseUser);
  }
}

-----> "database.dart"

class DatabaseService {
  // collection reference is just reference for certain collection
  final CollectionReference homeCrispCollection = 
      Firestore.instance.collection('customers');

 final String uid;
  DatabaseService({this.uid});


// Cust data from snapshot
  CustData _custDataFromSnapshot(DocumentSnapshot snapshot) {
    return CustData(
      custId: uid,
      username: snapshot.data['username'],
      residence: snapshot.data['residence'],
      dateOfBirth: snapshot.data['dateOfBirth'],
    );
  }

  // Get user doc stream
  Stream<CustData> get custData {
    return homeCrispCollection
        .document(uid)
        .snapshots()
        .map(_custDataFromSnapshot);
  }
}

image 1 image 2 image 3

0 个答案:

没有答案