当我运行时出现以下错误。这些是扑入Firebase的基本代码。
在构建MyHomePage(dirty,state:MyHomePageState#b163a)时引发了以下断言: 'package:flutter / src / widgets / text.dart':断言失败:第235行pos 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(),
);
},
),
);
}
}
答案 0 :(得分:1)
您有一个文本控件,其中的文本为空。
在这里,您没有输入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")