-2
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/src/widgets/container.dart';
import 'package:flutter/src/widgets/framework.dart';
import 'package:safehandsv2/utils/quotes.dart';

class CustomAppBar extends StatelessWidget {
  //const CustomAppBar({super.key});

  Function? onTap;
  int? quoteIndex;
  CustomAppBar({this.onTap, this.quoteIndex});


  @override
  Widget build(BuildContext context) {
    return InkWell(
      onTap:() {
        **onTap!();**
      },
      child: Text(sweetSayings[quoteIndex!],
      
      style: TextStyle(fontSize: 22, ),
      
      ),

      
    );
  }
}

so here the error I'm getting is null not defined etc.

error - Exception has occurred.
_CastError (Null check operator used on a null value)
MendelG
  • 14,885
  • 4
  • 25
  • 52
Pranav
  • 29
  • 1

1 Answers1

1

Your function onTap is defined as nullable (the ?):

Function? onTap;

then, you're calling the function using the ! operator, which means to say "I won't be null" but when you called it, it was indeed null.

So, first, check if it's null before calling it:

onTap: () {
        if (onTap != null) {
          onTap!();
        }
      },

When your calling CustomAppBar you're probably not passing in an onTap, since it's optional.

See also

MendelG
  • 14,885
  • 4
  • 25
  • 52