Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 697
Suresh Avatar asked Jun 03 '18 14:06

Suresh


3 Answers

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.

like image 79
Ziaur Rahman Avatar answered Oct 12 '22 22:10

Ziaur Rahman


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"),
like image 29
Phuc Tran Avatar answered Oct 12 '22 22:10

Phuc Tran


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

like image 34
Vineeth Mohan Avatar answered Oct 12 '22 23:10

Vineeth Mohan