0

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)),
    );
  }
  }
2
  • which line exactly causes the error? Commented Jan 8, 2022 at 11:27
  • so the console points out the error as shown below(It points to the personal screen where I have created a sheet object).The relevant error-causing widget was: Sheet Sheet:file/splitzy_sushi/lib/screens/personal_screen.dart:17:12 Commented Jan 8, 2022 at 13:47

1 Answer 1

1

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:..)
Sign up to request clarification or add additional context in comments.

3 Comments

Hi, I tried your approach and unfortunately the problem still remains . Thanks for the response though :)
Are you getting error in the same line? Try updated answer also and also please add comment in your question which line is causing the error.
Pleased to help you and mark this as answer if worked for you and upvote too. thanks

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.