NoSuchMethodError : The getter 'title' was called on null

  1. Summary of my problem
    I am making a shopping app UI , i have a product class which have the list of products for the app , when i use( product.tittle, product.image, product.price) it is returning an error. I have used the similar code for other pages in this app and it worked just fined but now it is not working for the new page
  • Error Message
Performing hot reload...
Syncing files to device Android SDK built for x86 64...

════════ Exception caught by widgets library ═══════════════════════════════════════════════════════
The following NoSuchMethodError was thrown building Body(dirty, dependencies: [MediaQuery]):
The getter 'title' was called on null.
Receiver: null
Tried calling: title

The relevant error-causing widget was: 
  Body file:///home/nitin/AndroidStudioProjects/shopping/lib/Screens/home/details/details_screen.dart:33:13
When the exception was thrown, this was the stack: 
#0      Object.noSuchMethod (dart:core-patch/object_patch.dart:51:5)
#1      Body.build (package:shopping/Screens/home/details/body.dart:41:32)
#2      StatelessElement.build (package:flutter/src/widgets/framework.dart:4638:28)
#3      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4564:15)
#4      Element.rebuild (package:flutter/src/widgets/framework.dart:4280:5)
...
════════════════════════════════════════════════════════════════════════════════════════════════════
Reloaded 5 of 728 libraries in 948ms.
  1. Here’s the minimum code you would need to reproduce the problem -

body.dart file

import 'package:flutter/material.dart';
import 'package:flutter/rendering.dart';
import 'package:shopping/constants.dart';
import 'package:shopping/models/Product.dart';


class Body extends StatelessWidget {
  final Product product;
  const Body({Key key, this.product}) : super(key: key);
  @override
  Widget build(BuildContext context) {
    // it provides us total height and width
    Size size = MediaQuery.of(context).size;
    return SingleChildScrollView(
      child: Column(

        children: [
          SizedBox(
            height: size.height,
            child: Stack(
              children: [
                Container(
                  margin: EdgeInsets.only(top: size.height * 0.3),
                  height: 500,
                  decoration: BoxDecoration(
                      color: Colors.white,
                    borderRadius: BorderRadius.only(
                      topLeft: Radius.circular(24),
                      topRight: Radius.circular(24),
                    ),
                  ),
                ),
                Padding(
                  padding: const EdgeInsets.symmetric(horizontal: kDefaultPaddin),
                  child: Column(crossAxisAlignment: CrossAxisAlignment.start,
                    children: [
                      Text("Aristocratic Hand Bag",
                      style: TextStyle(color: Colors.white),
                      ),
                     Text(
                       product.title,
                     style: Theme.of(context).textTheme.headline4.copyWith(
                       color: Colors.white, fontWeight: FontWeight.bold
                     ),),
                     Row(
                       children: [
                         RichText(text: TextSpan(
                           children: [
                             TextSpan(text: "Price\n"),
                             TextSpan(
                               text: "${product.price}",
                               style: Theme.of(context)
                                 .textTheme
                                 .headline4
                                 .copyWith(
                                 color: Colors.white,
                                 fontWeight: FontWeight.bold
                               )
                             ),

                           ]
                         ),),
                         SizedBox(width: kDefaultPaddin),
                         Expanded(child: Image.asset(product.image))
                       ],
                     )

                    ],
                  ),
                ),
              ],
            ),
          )
        ],
      ),
    );
  }
}

product.dart file -

import 'package:flutter/material.dart';
class Product {
  final String image, title, description;
  final int price, size, id;
  final Color color;
  Product({
    this.id,
    this.image,
    this.title,
    this.price,
    this.description,
    this.size,
    this.color,
  });
}

List<Product> products = [
  Product(
      id: 1,
      title: "Office Code",
      price: 234,
      size: 12,
      description: dummyText,
      image: "assets/images/bag_1.png",
      color: Color(0xFF3D82AE)),
  Product(
      id: 2,
      title: "Belt Bag",
      price: 234,
      size: 8,
      description: dummyText,
      image: "assets/images/bag_2.png",
      color: Color(0xFFD3A984)),
  Product(
      id: 3,
      title: "Hang Top",
      price: 234,
      size: 10,
      description: dummyText,
      image: "assets/images/bag_3.png",
      color: Color(0xFF989493)),
  Product(
      id: 4,
      title: "Old Fashion",
      price: 234,
      size: 11,
      description: dummyText,
      image: "assets/images/bag_4.png",
      color: Color(0xFFE6B398)),
  Product(
      id: 5,
      title: "Office Code",
      price: 234,
      size: 12,
      description: dummyText,
      image: "assets/images/bag_5.png",
      color: Color(0xFFFB7883)),
  Product(
    id: 6,
    title: "Office Code",
    price: 234,
    size: 12,
    description: dummyText,
    image: "assets/images/bag_6.png",
    color: Color(0xFFAEAEAE),
  ),
];

String dummyText =
    "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since. When an unknown printer took a galley.";

Body constructor was called as Body(); instead of Body(product:product);
To access title of the product, product must not be null.

2 Likes

it worked , thanks for the help

I am having the exact same problem. @Sandesh_Karki can you explain a little more your solution. When I tried your suggestion, I get an error. That product: product can not be assigned as the parameter for Body.