Home > database >  Flutter, type 'Null' is not a subtype of type 'String'
Flutter, type 'Null' is not a subtype of type 'String'

Time:01-26

I am new to flutter. I am making an UI but get stuck on the following error... The red screen on the background and the text "type 'Null' is not a subtype of type 'String'" on the screen. I don't know why it is displaying. Please guide me what I'm doing wrong here so that I could fix it.

Thanks for the help.

import 'package:flutter/material.dart';
import '../../../../../reusableWidget.dart';

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

  @override
  State<FarmerSellButton> createState() => _FarmerSellButtonState();
}

class _FarmerSellButtonState extends State<FarmerSellButton> {
  var farmerSellButtonList = [
    {
      "name": "Buyer/Company - 1",
      "delivery Time": "2 Days",
      "quantity": "30 Kg",
      "price": "Expected Price",
    },
    {
      "name": "Buyer/Company - 2",
      "delivery Time": "2 Days",
      "quantity": "40 Kg",
      "price": "Expected Price",
    },
    {
      "name": "Buyer/Company - 3",
      "delivery Time": "12 Days",
      "quantity": "60 Kg",
      "price": "Expected Price",
    },
    {
      "name": "Buyer/Company - 4",
      "delivery Time": "10 Days",
      "quantity": "50 Kg",
      "price": "Expected Price",
    },
    {
      "name": "Buyer/Company - 5",
      "delivery Time": "2 Days",
      "quantity": "90 Kg",
      "price": "Expected Price",
    },
    {
      "name": "Buyer/Company - 6",
      "delivery Time": "5 Days",
      "quantity": "50 Kg",
      "price": "Expected Price",
    },
    {
      "name": "Buyer/Company - 7",
      "delivery Time": "6 Days",
      "quantity": "100 Kg",
      "price": "Expected Price",
    },
  ];

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: reusableAppBar("ShubhChintak", false, true),
      body: Container(
        padding: EdgeInsets.only(
          top: MediaQuery.of(context).size.height * 0.01,
          left: MediaQuery.of(context).size.width * 0.02,
          right: MediaQuery.of(context).size.width * 0.02,
          bottom: MediaQuery.of(context).size.height * 0.01,
        ),
        child: GridView.builder(
          itemCount: farmerSellButtonList.length,
          gridDelegate:
              SliverGridDelegateWithFixedCrossAxisCount(crossAxisCount: 1),
          itemBuilder: (BuildContext context, int index) {
            return FarmerSellButtonList(
              CompanyName: farmerSellButtonList[index]["name"],
              DeliveryTime: farmerSellButtonList[index]["delivery"],
              Quantity: farmerSellButtonList[index]["quantity"],
              Price: farmerSellButtonList[index]["price"],
            );
          },
        ),
      ),
    );
  }
}

class FarmerSellButtonList extends StatelessWidget {
  final CompanyName;
  final DeliveryTime;
  final Quantity;
  final Price;

  const FarmerSellButtonList({
    Key? key,
    this.CompanyName,
    this.DeliveryTime,
    this.Quantity,
    this.Price,
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return SizedBox(
      height: MediaQuery.of(context).size.height * 0.1,
      child: Align(
        alignment: Alignment.center,
        child: Card(
          shadowColor: Colors.blue,
          shape: RoundedRectangleBorder(
              borderRadius: BorderRadius.circular(20),
              side: const BorderSide(color: Colors.blueAccent)),
          elevation: 5,
          semanticContainer: true,
          clipBehavior: Clip.antiAliasWithSaveLayer,
          child: GridTile(
            child: ListTile(
              title: reusableText(CompanyName, 18, TextAlign.start),
              subtitle: reusableText(DeliveryTime, 12, TextAlign.start),
              trailing: FittedBox(
                child: Column(
                  children: [
                    reusableText(Quantity, 12, TextAlign.end),
                    reusableText(Price, 12, TextAlign.end),
                  ],
                ),
              ),
            ),
          ),
        ),
      ),
    );
  }
}

CodePudding user response:

This error message is indicating that there is a variable, or a property of a variable, that is expecting a value of type 'String' but is receiving a value of type 'Null'. It's caused by an issue in this line:

"delivery Time": "2 Days",

It should be:

"delivery": "2 Days",

Also in class _FarmerSellButtonState, the following line is trying to access the "delivery" property of the farmerSellButtonList, but it's not found because "delivery" is not a property of the map.

"DeliveryTime: farmerSellButtonList[index]["delivery"], "

It should be:

"DeliveryTime: farmerSellButtonList[index]["delivery Time"], "
  • Related