Home > Software engineering >  Why snap becomes null when I use it in the Text() and when I print it, works fine
Why snap becomes null when I use it in the Text() and when I print it, works fine

Time:01-10

Below when I am printing print(snap['x']); this works fine and gives me the data from firebase but when I am using the same data bellow title: Text(snap['x']!), it a circular progress is shows. please tell me some way to take the value of snap['x'], to title: Text(snap['x']!), It will be very helpful.

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

class ListPage extends StatefulWidget {
  const ListPage({super.key});

  @override
  State<ListPage> createState() => _ListPageState();
}

class _ListPageState extends State<ListPage> {
  @override
  Widget build(BuildContext context) {
    var snap;
    DocumentReference docRef = FirebaseFirestore.instance
        .collection('fruitsAndVegetable')
        .doc('0 Apple');
    docRef.get().then((DocumentSnapshot snapshot) async {
      print('================================================================');
      print(snapshot.data());
      if (snapshot.exists) {
        snap = snapshot.data();
        print(snap['x']);
      }
    });

    return Scaffold(
        appBar: AppBar(
          title: Text("some data"),
        ),
        body: snap != null
            ? Column(
                children: [
                  ListTile(
                    title: Text(snap['x']!),
                  ),
                  ListTile(
                    title: Text(snap['id']!),
                  ),
                  ListTile(
                    title: Text(snap['name']!),
                  ),
                ],
              )
            : Center(
                child: Center(
                  child: CircularProgressIndicator(),
                ),
              ));
  
  }
}

CodePudding user response:

the snap widget you specified will be on the build and its value is setState(() { snap = snapshot.data(); }); I hope it helped you use it as

Setstate is work only inside statefulwidget

CodePudding user response:

Try Text(snap['x'].toString())

CodePudding user response:

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_core/firebase_core.dart';
import 'package:flutter/material.dart';

class ListPage extends StatefulWidget {
  const ListPage({super.key});

  @override
  State<ListPage> createState() => _ListPageState();
}

class _ListPageState extends State<ListPage> {
  late var snap;
  @override
  Widget build(BuildContext context) {
    DocumentReference docRef = FirebaseFirestore.instance
        .collection('fruitsAndVegetable')
        .doc('0 Apple');
    docRef.get().then((DocumentSnapshot snapshot) async {
      print('================================================================');
      print(snapshot.data());
      if (snapshot.exists) {
setState((){
        snap = snapshot.data();
});
        print(snap['x']);
      }
    });

    return Scaffold(
        appBar: AppBar(
          title: Text("some data"),
        ),
        body: snap != null
            ? Column(
                children: [
                  ListTile(
                    title: Text(snap['x']!),
                  ),
                  ListTile(
                    title: Text(snap['id']!),
                  ),
                  ListTile(
                    title: Text(snap['name']!),
                  ),
                ],
              )
            : Center(
                child: Center(
                  child: CircularProgressIndicator(),
                ),
              )
);
}
}

if you copy this code it will be work

  • Related