How to solve: Flutter , type 'Null' is not a subtype of type 'String'


Asked by Khaled Y on January 02, 2022 (source).

I am getting this error and I don't know why, type 'Null' is not a subtype of type 'String' The relevant error-causing widget was StreamBuilder<QuerySnapshot<Object?>>

import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:flutter/material.dart';
import 'package:market_app/data/Models/single_product_class.dart';
import 'package:market_app/persentation/menu_screens/add_windows_screen.dart';

class WindowsScreen extends StatefulWidget {
  const WindowsScreen({Key? key}) : super(key: key);

  State<WindowsScreen> createState() => _WindowsScreenState();

class _WindowsScreenState extends State<WindowsScreen> {
  CollectionReference windowsCollection =
  List<Product> window = [];
  // WindowsProvider().windows;

  Widget build(BuildContext context) {
    return Scaffold(
      floatingActionButton: FloatingActionButton(
        onPressed: () {
              builder: (contextt) => const AddWindowsScreen()));
        child: const Icon(Icons.add),
        backgroundColor: Colors.blueAccent,
      body: StreamBuilder<QuerySnapshot>(
          stream: windowsCollection.snapshots(),
          builder: (context, snapshot) {
            for (var document in!.docs) {
              Map<String, dynamic> productFromFStore =
        ! as Map<String, dynamic>;
              // =;

            if (snapshot.connectionState == ConnectionState.waiting) {
              return const Center(
                child: CircularProgressIndicator(),
            } else if (snapshot.connectionState == ConnectionState.done &&
       == null) {
              return const Center(
                child: Text(" no data to be showen "),
            } else if (snapshot.hasError) {
              return const Text('Something went wrong');

            return GridView.builder(
              gridDelegate: const SliverGridDelegateWithFixedCrossAxisCount(
                  crossAxisCount: 2),
              itemBuilder: (context, i) => GridTile(
                header: ListTile(
                  leading: IconButton(
                    icon: const Icon(Icons.favorite),
                    onPressed: () {},
                  trailing: IconButton(
                    icon: const Icon(Icons.shopping_cart_outlined),
                    onPressed: () {},
                    fit: BoxFit.cover),

                // footer: Row(
                //   children: [
                //     Text(windows[i].name),
                //     const Spacer(
                //       flex: 1,
                //     ),s
                //     // Text(windows[i].price)
                //   ],
                // ),

This is product model:

  import 'package:cloud_firestore/cloud_firestore.dart';

class Product {
  final String name;
  final String description;
  late String id;
  final double price;
  final int amount;
  final String imageUrl;

      required this.description,
      required this.price,
      required this.amount,
      required this.imageUrl,

  factory Product.fromJson(Map<String, dynamic> data) {
    return Product(
      name: data["name"] ,
      description: data["description"],
      price: double.parse(data["price"]),
      amount: int.parse(data["amount"]),
      imageUrl: data["imageUrl"],
      id: data["id"],
  factory Product.fromSnapshot(DocumentSnapshot snap) {
    Map<String, dynamic> data =! as Map<String, dynamic>;
    return Product(
      name: data["name"],
      amount: int.parse(data["amount"]),
      description: data["description"],
      imageUrl: data["imageUrl"],
      price: double.parse(data["price"]),
      id: data["id"],

  Map<String, dynamic> toMap(Product product) {
    return {
      "name": name,
      "description": description,
      "price": price,
      "amount": amount,
      "imageUrl": imageUrl,
      "id": id,

I have tried other ways to get the data like // child: Image. network(!.docs[index].data().toString()) but I have got only String, still, I don't know how to fix it, so I need some help


Question answered by JAYDEEP C (source).

Yes, dear null gives you an error in Flutter. you need to assign any value to the variable. It will solve your error. It gives an error because you use QuerySnapshot Object and it requires some value. If the value is null it will give you an error. Assign any value to it to solve the error.