简体   繁体   中英

How to Get Simple Data From Cloud Firestore For Flutter App?

When I Run getting following error. These are basic code for flutter into firebase.

The following assertion was thrown building MyHomePage(dirty, state: MyHomePageState#b163a): 'package:flutter/src/widgets/text.dart': Failed assertion: line 235 pos 15: 'data != null': is not

Please, Can Anyone Help me How to Resolve This?

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(),
          );
        },
      ),
    );
  }
}

You have a Text widget with null text.

Here, you did not enter title property for MyHomePage

class MyApp extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return MaterialApp(
      title: 'Firebase',
      home: MyHomePage(),
    );
  }
}

So this Text widget has null text value.

  appBar: AppBar(
    title: Text(widget.title),
  ),

To solve this, declare MyHomePage like this:

MyHomePage(title: "My Home Page")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM