【问题标题】:Flutter Firebase | The relevant error-causing widget was StreamBuilder<DocumentSnapshot<Object?>>颤振火力基地 |相关的导致错误的小部件是 StreamBuilder<DocumentSnapshot<Object?>>
【发布时间】:2021-11-16 06:51:59
【问题描述】:

我是 Flutter 初学者,我正在尝试使用 Streambuilder 从 Firestore 中检索数据。上周代码运行良好,本周我无法显示 Firestore 中的用户数据。

我得到的错误是:

type 'Null' 不是 type 'String' 的子类型 相关的导致错误的小部件是 StreamBuilder>

一个 RenderFlex 在右侧溢出了 99833 像素。 相关的导致错误的小部件是 行

这是我的代码

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:flutter/material.dart';
import 'package:flutter_application_1/main_views/account_method.dart';

class SettingsPage extends StatefulWidget {
  @override
  _SettingsPageState createState() => _SettingsPageState();
}

FirebaseAuth _auth = FirebaseAuth.instance;
final uid = _auth.currentUser!.uid;

class _SettingsPageState extends State<SettingsPage> {
  String errorMessage = '';

  @override
  Widget build(BuildContext context) {
    var boldFont = TextStyle(fontFamily: 'Inter', fontWeight: FontWeight.w600);
    final Stream<DocumentSnapshot<Map<String, dynamic>>> db =
        FirebaseFirestore.instance
        .collection('users')
        .doc(uid)
        .collection('Personal details')
        .doc()
        .snapshots();

    return Scaffold(
      appBar: AppBar(
        titleSpacing: 30,
        automaticallyImplyLeading: false,
        backgroundColor: Color.fromRGBO(1, 67, 55, 1),
        toolbarHeight: 100,
        title: new Text(
          'Settings',
          style: TextStyle(
              color: Color.fromRGBO(255, 255, 255, 1),
              fontFamily: 'Poppins',
              fontSize: 25,
              letterSpacing: 1.2,
              fontWeight: FontWeight.bold,
              height: 1),
        ),
      ),
      body: SingleChildScrollView(
        child: Container(
          padding: EdgeInsets.all(30),
          color: Color.fromRGBO(246, 246, 246, 1),
          child: Column(children: [
            SizedBox(
              height: 30,
            ),
            Container(
                padding: EdgeInsets.only(left: 30, right: 30),
                decoration: BoxDecoration(),
                child: Text('Account',
                    textAlign: TextAlign.center,
                    style: TextStyle(
                        fontSize: 24,
                        fontWeight: FontWeight.bold,
                        fontFamily: 'Inter'))),
            Padding(
              padding: EdgeInsets.all(30.0),
              child: Column(
                children: [
                  SizedBox(
                    height: 20,
                  ),
                  Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('First name'),
                        SizedBox(width: 40),
                        StreamBuilder<DocumentSnapshot>(
                          stream: db,
                          builder: (BuildContext context,
                              AsyncSnapshot<DocumentSnapshot> snapshot) {
                            if (snapshot.hasError)
                              return Text('Something went wrong');
                            if (snapshot.connectionState ==
                                ConnectionState.waiting)
                              return CircularProgressIndicator();

                            dynamic data = snapshot.data!.data();
                            return Text(data['First name']);
                          },
                        ),
                      ]),
                  Divider(color: Colors.black),
                  SizedBox(
                    height: 10,
                  ),
                  Row(
                    mainAxisAlignment: MainAxisAlignment.spaceBetween,
                    children: [
                      Text('Last name'),
                      SizedBox(width: 40),
                      StreamBuilder<DocumentSnapshot>(
                        stream: db,
                        builder: (BuildContext context,
                            AsyncSnapshot<DocumentSnapshot> snapshot) {
                          if (snapshot.hasError)
                            return Text('Something went wrong');
                          if (snapshot.connectionState ==
                              ConnectionState.waiting)
                            return CircularProgressIndicator();

                          dynamic data = snapshot.data!.data();
                          return Text(data['Last name'], style: boldFont);
                        },
                      ),
                    ],
                  ),
                  Divider(color: Colors.black),
                  SizedBox(
                    height: 10,
                  ),
                  InkWell(
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('Change password'),
                        SizedBox(width: 40),
                        Icon(Icons.arrow_forward, size: 18, color: Colors.black)
                      ],
                    ),
                  ),
                  Divider(color: Colors.black),
                  SizedBox(
                    height: 10,
                  ),
                  InkWell(
                    onTap: () {
                      Navigator.push(
                        context,
                        MaterialPageRoute(
                            builder: (context) => AccountMethod()),
                      );
                    },
                    child: Row(
                      mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      children: [
                        Text('Accounts'),
                        SizedBox(width: 40),
                        Icon(
                          Icons.arrow_forward,
                          color: Colors.black,
                          size: 19.0,
                        ),
                      ],
                    ),
                  ),
                  Divider(color: Colors.black),
                  SizedBox(
                    height: 30,
                  ),
                  ElevatedButton(
                      style: ButtonStyle(
                        backgroundColor: MaterialStateProperty.all(
                            Color.fromRGBO(1, 67, 55, 1)),
                        shape:
                            MaterialStateProperty.all<RoundedRectangleBorder>(
                                RoundedRectangleBorder(
                                    borderRadius: BorderRadius.circular(20))),
                      ),
                      child: Text('Log out'),
                      onPressed: () async {
                        try {
                          await FirebaseAuth.instance.signOut();
                          errorMessage = '';
                        } on FirebaseAuthException catch (error) {
                          errorMessage = error.message!;
                        }
                        setState(() {});
                        Navigator.of(context).pushReplacementNamed('/signIn');
                      }),
                  SizedBox(height: 15),
                ],
              ),
            )
          ]),
        ),
      ),
    );
  }
}

这是firestore db的屏幕截图。

firestore db screenshot

感谢您提前提供的任何帮助

【问题讨论】:

  • 发布完整的 SettingsPage 代码。某些东西正在返回null,它应该返回String(可能是Last name)。确认用户文档有字段Last name(大小写完全一致,中间有空格)。同时发布用户的文档(firestore 截图)。
  • 嗨,彼得,我已经在 firestore 上发布了完整的设置页面代码和用户文档。我可以确认该字段是按原样输入的

标签: firebase flutter dart google-cloud-firestore


【解决方案1】:

好的,我认为问题出在此处。

final Stream<DocumentSnapshot<Map<String, dynamic>>> db =
  FirebaseFirestore.instance
  .collection('users')
  .doc(uid)
  .collection('Personal details')
  .doc() // <-- error should be from here.
  .snapshots();

您正在尝试流式传输不存在的文档引用。 .doc() 返回一个不存在的文档引用。而是使用.doc('Path_to_the_document')

所以添加包含Last name字段的文档的路径。

【讨论】:

  • 很高兴能帮上忙。
猜你喜欢
  • 2022-11-14
  • 2021-06-05
  • 2021-07-03
  • 2021-03-10
  • 2021-03-19
  • 2021-02-03
  • 2020-06-16
  • 2020-06-26
  • 2021-01-30
相关资源
最近更新 更多