skip to Main Content

I am taking data from FireStore collection but this is showing issue.

I am trying this-

class _HomePageState extends State<HomePage> {
  AuthClass authClass = AuthClass();
  final FirebaseAuth auth = FirebaseAuth.instance;

  @override
  Widget build(BuildContext context) {
    String st = "ToDo${auth.currentUser?.uid}";
    final Stream<QuerySnapshot> stream =
        FirebaseFirestore.instance.collection(st).snapshots();

    return Scaffold(
      backgroundColor: Colors.black87,
      appBar: AppBar(
        backgroundColor: Colors.black87,
        title: Text(
          "Today's Schedule",
          style: TextStyle(
            fontSize: 34,
            fontWeight: FontWeight.bold,
            color: Colors.white,

…………………………………………………………………………………………………………..

body: StreamBuilder(
          stream: stream,
          builder: (context, snapshot) {
            return ListView.builder(
                itemCount: snapshot.data?.docs.length,
                itemBuilder: (context, index) {
                  IconData iconData;
                  Color iconColor;
                  Map<String, dynamic> document =
                      snapshot.data?.docs[index].data() as Map<String, dynamic>;
                  switch (document["Category"]) {
                    case "Work":
                      iconColor = Colors.red;
                      iconData = Icons.run_circle_outlined;
                      break;
                    case "WorkOut":
                      iconColor = Colors.teal;
                      iconData = Icons.alarm;
                      break;

There is data available on Firestore still its showing Null subtype. My app is working but at some sort of time its giving an error.

2

Answers


  1. Chosen as BEST ANSWER

    I got answer to this.

    body: StreamBuilder(
              stream: stream,
              builder: (context, snapshot) {
                return ListView.builder(
                    itemCount: snapshot.data?.docs.length,
                    itemBuilder: (context, index) {
                      IconData iconData;
                      Color iconColor;
                      Map<String, dynamic?> document =
                          snapshot.data?.docs[index].data() as Map<String, dynamic?>;
                      switch (document["Category"]) {
                        case "Work":
                          iconColor = Colors.red;
                          iconData = Icons.run_circle_outlined;
                          break;
                        case "WorkOut":
                          iconColor = Colors.teal;
                          iconData = Icons.alarm;
                          break;
    
    
    
        Map<String, dynamic?> document =   snapshot.data?.docs[index].data() as Map<String, dynamic?>; 
    

    here in dynamic i decleared as a nullable value then it worked.


  2. It may be due to 2 reason.

    First is by adding a data with your stream type in initialData of stream.

    Second is in your snapShot.data contain null value. Check from error console and can fix in the model file Eg: json["address"] ?? '' like that.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search