Suresh
Suresh

Reputation: 5997

Flutter error - failed assertion: line 213 pos 15: 'data != null': is not true at the time of fetching data from firestore

Working on Android app using flutter. Trying to fetch documents from firestore & show on the screen through a widget. Here is my code...

import 'dart:async';
import 'dart:io';
import 'package:flutter/material.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:image_picker/image_picker.dart';
import 'package:firebase_storage/firebase_storage.dart';
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:http/http.dart' as http;
import 'dart:convert';

class HomePage extends StatefulWidget {
  @override
  HomePageState createState() => new HomePageState();
}

class HomePageState extends State<HomePage> {

  @override
  void initState() {
    super.initState();
  }



  @override
  Widget build(BuildContext context) {

    Widget userTimeline = new Container(
        margin: const EdgeInsets.only(top: 30.0, right: 20.0, left: 20.0),
        child: new Row(
          children: <Widget>[
            new Expanded(
                child: new Column(
              children: <Widget>[
                new StreamBuilder<QuerySnapshot>(
                  stream: Firestore.instance.collection('tripsDocs').snapshots(),
                  builder: (BuildContext context, AsyncSnapshot<QuerySnapshot> snapshot) {
                    if (!snapshot.hasData) return new Text('Loading...');

                    new ListView(
                      children: snapshot.data.documents.map((DocumentSnapshot document) {
                        new ListTile(
                          title: document['docTitle'] != null? new Text(document['docTitle']) : new Text("Hello"),
                          subtitle: new Text('Suresh'),
                        );
                      }).toList(),
                    );
                  },
                )
              ],
            ))
          ],
        ));

    return new Scaffold(

      body: new ListView(
        children: <Widget>[
          userTimeline,
        ],
      ),

    );

  }
}

But, whenever I'm executing this widget I'm getting following error...

'package:flutter/src/widgets/text.dart': Failed assertion: line 213 pos 15: 'data != null': is not true

Can't able to understand what's going wrong.

Upvotes: 16

Views: 84026

Answers (5)

hary say
hary say

Reputation: 1

make sure the returned form of the snapshot you get is json that matches what was read. If the return from the snapshot is in the form of an array [] then it must match, if it is an array object [{ }] then it must also match

Upvotes: 0

Migue SK
Migue SK

Reputation: 1

change the name of the collection in the cloud firestore and in each query you have in the project.

that will change the name on the firebase page, at least that solved my problem

stream: Firestore.instance.collection("colection").snapshots(),

to

stream: Firestore.instance.collection("collection").snapshots(),

Upvotes: -1

Ziaur Rahman
Ziaur Rahman

Reputation: 1188

if your data is not null, then run the command on your project terminal flutter clean. It may remove your error. I have similar issue.

Upvotes: 2

Vineeth Mohan
Vineeth Mohan

Reputation: 2864

This error is because you are trying to add some null data to Text. for adding text Content to Text widget dynamically better to setState of text place where you are getting that data. Delay for getting content cause this error. so a condition check for data is also good.

@override
  void initState() {
setState(() {
       docText=document['docTitle'];
      });

    super.initState();
  }

....

title: new Text( docText == null? 'Loading ..',document['docTitle']),

try this way

Upvotes: 2

Phuc Tran
Phuc Tran

Reputation: 8093

Here is the constructor of Text

const Text(this.data, {
    Key key,
    this.style,
    this.textAlign,
    this.textDirection,
    this.softWrap,
    this.overflow,
    this.textScaleFactor,
    this.maxLines,
  }) : assert(data != null),
       textSpan = null,
       super(key: key);

With

final String data;

As you can see, data is a required field and it must be not null.

You can use below code in case your data could be null

title: document['docTitle'] != null? new Text(document['docTitle']) : new Text("Hello"),

Upvotes: 2

Related Questions