使用Firebase创建数据后无法获取用户数据

时间:2020-09-07 04:14:55

标签: flutter google-cloud-firestore firebase-authentication

我正在尝试使用Firebase在我的Flutter应用中添加用户身份验证,但是在创建用户并尝试从Firestore文档中获取用户数据后,我遇到了问题。

创建用户后,出现以下错误消息:

@php
   echo $birthDate = $patient->dob ?? 'No DOB' ;
@endphp

执行热重载后不会发生该错误,并且如果我使用现有用户登录也不会发生该错误。我相信在我设置“用户”集合中的数据之前,正在加载TabNavigationBar()屏幕,因为StreamBuilder中的main.dart文件中存在“ onAuthStateChanged”。但是我不知道如何确保仅在设置用户数据后才能加载。

这是我创建或登录用户的方式:

    <dependency>                                                                
      <groupId>javax.xml.bind</groupId>                                         
      <artifactId>jaxb-api</artifactId>                                         
    </dependency>

我的main.dart

E/flutter (30085): [ERROR:flutter/lib/ui/ui_dart_state.cc(157)] Unhandled Exception: NoSuchMethodError: The method '[]' was called on null.
E/flutter (30085): Receiver: null
E/flutter (30085): Tried calling: []("username")

我是这样从Firestore获取数据的:

      setState(() {
        _isLoading = true;
      });
      if (isLogin) {
        authResult = await _auth.signInWithEmailAndPassword(email: email, password: password);
      } else {
        authResult = await _auth.createUserWithEmailAndPassword(email: email, password: password);
        final ref = FirebaseStorage.instance.ref().child('user_image').child(authResult.user.uid + '.jpg');
        await ref.putFile(image).onComplete;
        final url = await ref.getDownloadURL();
        await Firestore.instance.collection('users').document(authResult.user.uid).setData({
          'username': username,
          'email': email,
          'image_url': url,
        });
      }

并在此处调用

Widget build(BuildContext context) {
    return ChangeNotifierProvider(
      create: (context) => User(),
      child: MaterialApp(
        title: 'Mobi',
        theme: appTheme(),
        home: StreamBuilder(
          stream: FirebaseAuth.instance.onAuthStateChanged,
          builder: (ctx, userSnapshot) {
            if (userSnapshot.connectionState == ConnectionState.waiting) {
              return SplashScreen();
            }
            if (userSnapshot.hasData) {
              return TabNavigationBar();
            } else {
              return AuthScreen();
            }
          },
        ),
      ),
    );
  }

1 个答案:

答案 0 :(得分:0)

我设法解决了我的问题。我在Firebase文档上使用了StreamBuilder和snapshots()函数,这给了我一个Stream。

final.data.raw <- structure(
  list(RoyalPerth.Attendance = c(235, 209), RoyalPerth.Admissions = c(99, 97), RoyalPerth.Tri1 = c("8", "N/A"), 
       RoyalPerth.Tri2 = c(33, 41), RoyalPerth.Tri3 = c(89, 73), RoyalPerth.Tri4 = c(85, 80), 
       RoyalPerth.Tri5 = c("20", "14"), Fremantle.Attendance = c(155, 145), Fremantle.Admissions = c(70, 56), 
       Fremantle.Tri1 = c("N/A", "N/A"), Fremantle.Tri2 = c(25, 22), Fremantle.Tri3 = c(67, 51), 
       Fremantle.Tri4 = c(54, 47), Fremantle.Tri5 = c(9, 24), PrincessMargaret.Attendance = c(252, 219), 
       PrincessMargaret.Admissions = c(59,47), PrincessMargaret.Tri1 = c("N/A", "N/A"), PrincessMargaret.Tri2 = c("13", "14"), 
       PrincessMargaret.Tri3 = c(75, 61), PrincessMargaret.Tri4 = c(159, 139), PrincessMargaret.Tri5 = c("4", "4"), 
       KingEdward.Attendance = c(52, 43), KingEdward.Admissions = c("6", "7"), KingEdward.Tri1 = c("N/A", "N/A"), 
       KingEdward.Tri2 = c("N/A", "N/A"), KingEdward.Tri3 = c("7", "N/A"), KingEdward.Tri4 = c(20, 25), 
       KingEdward.Tri5 = c("25", "17"), SirCharles.Attendance = c(209, 184), SirCharles.Admissions = c(109, 112), 
       SirCharles.Tri1 = c("N/A", "N/A"), SirCharles.Tri2 = c(42, 43), SirCharles.Tri3 = c(108, 73), 
       SirCharles.Tri4 = c(47, 61), SirCharles.Tri5 = c("11", "5"), Armadale.Attendance = c(166, 175), 
       Armadale.Admissions = c(19, 25), Armadale.Tri1 = c("N/A", "N/A"), Armadale.Tri2 = c(16, 26), 
       Armadale.Tri3 = c(62, 73), Armadale.Tri4 = c(79, 55), Armadale.Tri5 = c("9", "19"), 
       Swan.Attendance = c(133, 129), Swan.Admissions = c(17, 25), Swan.Tri1 = c("N/A", "N/A"), 
       Swan.Tri2 = c(29, 25), Swan.Tri3 = c(59, 57), Swan.Tri4 = c(42, 43), 
       Swan.Tri5 = c("N/A", "4"), Rockingham.Attendance = c(155, 145), Rockingham.Admissions = c("10", "24"), 
       Rockingham.Tri1 = c("N/A", "N/A"), Rockingham.Tri2 = c(12, 26), Rockingham.Tri3 = c(51, 45), 
       Rockingham.Tri4 = c(81, 65), Rockingham.Tri5 = c("11", "8"), Joondalup.Attendance = c(267, 241), 
       Joondalup.Admissions = c(73, 81), Joondalup.Tri1 = c("N/A", "N/A"), Joondalup.Tri2 = c(27, 23), 
       Joondalup.Tri3 = c(75, 78), Joondalup.Tri4 = c(151, 133), Joondalup.Tri5 = c("12", "7")), 
  row.names = 1:2, class = "data.frame")