【发布时间】: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的屏幕截图。
感谢您提前提供的任何帮助
【问题讨论】:
-
发布完整的 SettingsPage 代码。某些东西正在返回
null,它应该返回String(可能是Last name)。确认用户文档有字段Last name(大小写完全一致,中间有空格)。同时发布用户的文档(firestore 截图)。 -
嗨,彼得,我已经在 firestore 上发布了完整的设置页面代码和用户文档。我可以确认该字段是按原样输入的
标签: firebase flutter dart google-cloud-firestore