如何从 Cloud Firestore 获取 Flutter App 的简单数据?
How to Get Simple Data From Cloud Firestore For Flutter App?
当我 运行 出现以下错误时。这些是 flutter 进入 Firebase 的基本代码。
构建 MyHomePage 时引发了以下断言(脏,状态:MyHomePageState#b163a):
'package:flutter/src/widgets/text.dart':断言失败:第 235 行第 15 行:'data != null':不是
拜托,谁能帮我解决这个问题?
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
Future<void> main() async {
final FirebaseApp app = await FirebaseApp.configure(
name: 'vscodefirebase',
options: const FirebaseOptions(
googleAppID: 'xxx',
gcmSenderID: 'xxx',
apiKey: 'xxxx',
projectID: 'vscodefirebase',
),
);
final Firestore firestore = Firestore(app: app);
await firestore.settings(timestampsInSnapshotsEnabled: true);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('books').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['title']),
subtitle: new Text(document['author']),
);
}).toList(),
);
},
),
);
}
}
您有一个包含空文本的文本小部件。
在这里,您没有为 MyHomePage
输入标题 属性
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase',
home: MyHomePage(),
);
}
}
因此此文本小部件具有空文本值。
appBar: AppBar(
title: Text(widget.title),
),
要解决这个问题,请像这样声明 MyHomePage:
MyHomePage(title: "My Home Page")
当我 运行 出现以下错误时。这些是 flutter 进入 Firebase 的基本代码。
构建 MyHomePage 时引发了以下断言(脏,状态:MyHomePageState#b163a): 'package:flutter/src/widgets/text.dart':断言失败:第 235 行第 15 行:'data != null':不是
拜托,谁能帮我解决这个问题?
import 'dart:async';
import 'package:flutter/material.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
Future<void> main() async {
final FirebaseApp app = await FirebaseApp.configure(
name: 'vscodefirebase',
options: const FirebaseOptions(
googleAppID: 'xxx',
gcmSenderID: 'xxx',
apiKey: 'xxxx',
projectID: 'vscodefirebase',
),
);
final Firestore firestore = Firestore(app: app);
await firestore.settings(timestampsInSnapshotsEnabled: true);
runApp(MyApp());
}
class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase',
home: MyHomePage(),
);
}
}
class MyHomePage extends StatefulWidget {
MyHomePage({Key key, this.title}) : super(key: key);
final String title;
@override
MyHomePageState createState() => MyHomePageState();
}
class MyHomePageState extends State<MyHomePage> {
@override
Widget build(BuildContext context) {
return Scaffold(
appBar: AppBar(
title: Text(widget.title),
),
body: StreamBuilder<QuerySnapshot>(
stream: Firestore.instance.collection('books').snapshots(),
builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
if (!snapshot.hasData) return new Text('Loading...');
return new ListView(
children: snapshot.data.documents.map((DocumentSnapshot document) {
return new ListTile(
title: new Text(document['title']),
subtitle: new Text(document['author']),
);
}).toList(),
);
},
),
);
}
}
您有一个包含空文本的文本小部件。
在这里,您没有为 MyHomePage
输入标题 属性class MyApp extends StatelessWidget {
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Firebase',
home: MyHomePage(),
);
}
}
因此此文本小部件具有空文本值。
appBar: AppBar(
title: Text(widget.title),
),
要解决这个问题,请像这样声明 MyHomePage:
MyHomePage(title: "My Home Page")