Aayushi chauhan
Aayushi chauhan

Reputation: 1

Flutter -type 'Null' is not a subtype of type 'String', I have added null safety everywhere and still the error remains

I am new to flutter and I am trying out this new splitting /expense tracker app, but it keeps throwing me this error. I did a little research and I found out that I have to make sure that the String is not null by adding a question mark in front. I have done that and still the problem exsists . Please help me out. type 'Null' is not a subtype of type 'String'

sheet class
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:splitzy_sushi/constants.dart';
import 'package:splitzy_sushi/model/object_data.dart';
import 'package:splitzy_sushi/model/object_data.dart';
import 'package:splitzy_sushi/model/object_list.dart';
import 'package:splitzy_sushi/constants.dart';
import 'package:splitzy_sushi/screens/friend_screen.dart';


int index=0;

class Sheet extends StatefulWidget {
  String? bankName;
  Sheet({this.bankName});
  @override
  State<Sheet> createState() => _SheetState();
}

class _SheetState extends State<Sheet> {
  static String? taskName;
  static String? Amount;


  @override
  Widget build(BuildContext context) {

    return SafeArea(
      child: Scaffold(
        backgroundColor:kDarkBlue,
        body: Padding(
          padding: EdgeInsets.all(15.0),
          child: Column(
            children: [
              Text(bankName,style: TextStyle(color: green,fontWeight: FontWeight.bold,fontSize: 25.0),),
              Row(
                children: [
                  Text("TASK:",style: TextStyle(color: kGrey,fontWeight: FontWeight.bold),),
                  SizedBox(
                    width: 10.0,
                  ),
                  Expanded(
                    child: TextField(
                      style: TextStyle(color: Colors.white),
                      decoration: InputDecoration(
                          enabledBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                          ),
                          focusedBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.white),
                          ),
                          hintText: 'rent',
                          hintStyle: TextStyle(color: Colors.blueGrey.shade800)
                      ),
                      cursorColor: Colors.teal,
                      onChanged: (value){
                        taskName=value;
                      },
                    ),
                  ),
                ],
              ),
              Row(
                children: [
                  Text("AMOUNT:",style: TextStyle(color: kGrey,fontWeight: FontWeight.bold)),
                  SizedBox(
                    width: 10.0,
                  ),
                  Expanded(
                    child: TextField(
                      style: TextStyle(color: Colors.white),
                      decoration: InputDecoration(
                          enabledBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.grey),
                          ),
                          focusedBorder: UnderlineInputBorder(
                            borderSide: BorderSide(color: Colors.white),
                          ),
                          hintText: '6000',
                          hintStyle: TextStyle(color: Colors.blueGrey.shade800)
                      ),
                      keyboardType: TextInputType.number,
                      cursorColor: Colors.teal,
                      onChanged: ( newval){
                        Amount=(newval);
                      },
                    ),
                  ),
                ],
              ),
              FlatButton(onPressed:(){
                Provider.of<ObjectData>(context,listen: false).add(taskName,Amount);
                print(Provider.of<ObjectData>(context,listen: false).objectList);
              }
                , child: Text("ADD",style: TextStyle(color: green,fontWeight: FontWeight.bold,fontSize: 20.0),),
                highlightColor: Colors.blueGrey,),
              Expanded(
                child: Container(
                  child: ObjectList(),
                ),
              )
            ],
          ),
        ),
      ),
    );
  }
}



PERSONAL_SCREEN CLASS
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:splitzy_sushi/constants.dart';
import 'package:splitzy_sushi/model/object_data.dart';
import 'package:splitzy_sushi/model/object_data.dart';
import 'package:splitzy_sushi/model/object_list.dart';
import 'package:splitzy_sushi/constants.dart';
import 'package:splitzy_sushi/model/sheet.dart';
class PersonalScreen extends StatefulWidget {
  @override
  State<PersonalScreen> createState() => _PersonalScreenState();
}

class _PersonalScreenState extends State<PersonalScreen> {
  @override
  Widget build(BuildContext context) {
    return Sheet(bankName:"personal");
  }
  }

OBJECT  CLASS
import 'package:flutter/material.dart';
class Object{
  Stringa name;
  String amount;
  Object({required this.name,required this.amount});
}

OBJECT LIST CLASS
import 'package:flutter/material.dart';
import 'object_data.dart';
import 'package:provider/provider.dart';
import 'object_tile.dart';
import 'package:splitzy_sushi/constants.dart';

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

  @override
  State<ObjectList> createState() => _ObjectListState();
}

class _ObjectListState extends State<ObjectList> {
  @override
  Widget build(BuildContext context) {
    return Container(
      child: ListView.builder(
        scrollDirection: Axis.vertical,
        shrinkWrap: true,
        itemBuilder: (BuildContext context, int index) {
          return ObjectTile(index: index);

        },
        itemCount: Provider.of<ObjectData>(context).objectList.length,
      ),
    );
  }
}


OBJECT DATA CLASS 
import 'package:flutter/material.dart';
import 'package:provider/provider.dart';
import 'package:splitzy_sushi/model/object.dart';

class ObjectData extends ChangeNotifier{

  List<Object>objectList=[
    Object(name:"Task Name" , amount:"Amount"),
    Object(name: "dog",amount:"56")
  ];

  void add(string, money){
    objectList.add(Object(name: string, amount: money)
    );
    notifyListeners();
  }
  void delete(Object obj){
    objectList.remove(obj);
    notifyListeners();
  }

}

OBJECT TILE CLASS
import 'package:flutter/material.dart';
import 'object_data.dart';
import 'package:provider/provider.dart';
import 'package:splitzy_sushi/constants.dart';

class ObjectTile extends StatelessWidget {
  int index;
  // final Function()? onPress;
 ObjectTile({required this.index});

  @override
  Widget build(BuildContext context) {
    return ListTile(
      // onLongPress:Provider.of<ObjectData>(context).delete(Provider.of(context).objectList[index]),
      leading: Text(
          Provider.of<ObjectData>(context).objectList[index].name,
          style: TextStyle(color: kGrey, fontWeight: FontWeight.bold)),
      trailing: Text(
          Provider.of<ObjectData>(context).objectList[index].amount,
          style: TextStyle(color: kGrey, fontWeight: FontWeight.bold)),
    );
  }
  }

Upvotes: 0

Views: 639

Answers (1)

Diwyansh
Diwyansh

Reputation: 3514

It seems that you are not getting the value in bankName variable and it's passing a null value so your Text widget is throwing this error.

I think you come to this screen bankName must not be null so just remove ? while declaring the variable and also curly braces from Constructorlike below :

String bankName;

Sheet(this.bankName);

or try doing so

Text(bankName ?? "",style:..)

Upvotes: 1

Related Questions