Home > Software design >  Flutter Provider Null check operator used on a null value
Flutter Provider Null check operator used on a null value

Time:04-07

I did a very simple provider test, but I keep getting this error. I switched to flutter stable channel, upgraded, and cleaned it. the result is the same. It gives this error when I write the function to dispose method of any screen.

My purpose for using the dipsos method:

Disposal of ValueNotifiers: It's always a good practice to dispose all the ValueNotifiers once they are no longer in use which can save the app from memory loss.

I want current subscriptions to expire when I exit the screen for performance. I create a new valuenotifier when the screen is initialize and I want to dispose when I exit the screen.

main.dart file

void main() async {
  WidgetsFlutterBinding.ensureInitialized();
  await Firebase.initializeApp(
    options: DefaultFirebaseOptions.currentPlatform,
  );
  runApp(const MyApp());
}

class MyApp extends StatelessWidget {
  const MyApp({Key? key}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return MultiProvider(
      providers: [
        ChangeNotifierProvider(create: (context) => ScreenChangeNotifier(),),
        Provider(create: (context) => Genel(),)
      ],
      child: MaterialApp(
        theme: ThemeData(
            primarySwatch: Colors.blue
        ),
        title: 'Test',
        home: HomeScreen(),

      ),
    );
  }
}

ScreenChangeNotifier.dart file

class ScreenChangeNotifier extends ChangeNotifier
{
    void hello() => print("say hello");
}

HomeScreen.dart file

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void dispose() {
    context.read<ScreenChangeNotifier>().hello();
    super.dispose();
  }
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
    );
  }
}

this error

======== Exception caught by widgets library =======================================================
The following _CastError was thrown while finalizing the widget tree:
Null check operator used on a null value

When the exception was thrown, this was the stack: 
#0      Element.widget (package:flutter/src/widgets/framework.dart:3203:31)
#1      Provider._inheritedElementOf (package:provider/src/provider.dart:341:17)
#2      Provider.of (package:provider/src/provider.dart:293:30)
#3      ReadContext.read (package:provider/src/provider.dart:656:21)
#4      _HomeScreenState.dispose (package:bencebilirpro/screens/HomeScreen.dart:11:13)
#5      StatefulElement.unmount (package:flutter/src/widgets/framework.dart:4983:11)
#6      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1926:13)
#7      _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#8      SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#9      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#10     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#11     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#12     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#13     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#14     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#15     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#16     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#17     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#18     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#19     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#20     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#21     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#22     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#23     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#24     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#25     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#26     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#27     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#28     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#29     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#30     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#31     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#32     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#33     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#34     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#35     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#36     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#37     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#38     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#39     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#40     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#41     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#42     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#43     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#44     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#45     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#46     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#47     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#48     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#49     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#50     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#51     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#52     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#53     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#54     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#55     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#56     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#57     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#58     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#59     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#60     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#61     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#62     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#63     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#64     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#65     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#66     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#67     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#68     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#69     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#70     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#71     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#72     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#73     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#74     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#75     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#76     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#77     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#78     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#79     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#80     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#81     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#82     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#83     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#84     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#85     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#86     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#87     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#88     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#89     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#90     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#91     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#92     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#93     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#94     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#95     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#96     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#97     ListIterable.forEach (dart:_internal/iterable.dart:39:13)
#98     _InactiveElements._unmountAll (package:flutter/src/widgets/framework.dart:1935:25)
#99     BuildOwner.lockState (package:flutter/src/widgets/framework.dart:2519:15)
#100    BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2932:7)
#101    WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:884:19)
#102    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#103    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#104    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
#105    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
#109    _invoke (dart:ui/hooks.dart:151:10)
#110    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#111    _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)
====================================================================================================

CodePudding user response:

Why you are calling ScreenChangeNotifier in dispose function? You have to call it in initstate or inside build method.

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  @override
  void initState() {
    super.initState();
    context.read<ScreenChangeNotifier>().hello();
  }
  @override
  Widget build(BuildContext context) {
// or you can call it here..
 // context.read<ScreenChangeNotifier>().hello();
    return Scaffold(
      body: Container(),
    );
  }
}

CodePudding user response:

First of all, why you call your method in the dispose() method? dispose called when this object is removed from the tree permanently, you shouldn't try to find ancestors of this widget at this point because your widget is already deleted from the widget tree.

If you want to do that anyway, try to create variable in State, initialize it in initState() with context.read<ScreenChangeNotifier>() and in dispose call variable.hello()

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

  @override
  State<HomeScreen> createState() => _HomeScreenState();
}

class _HomeScreenState extends State<HomeScreen> {
  ScreenChangeNotifier _notifier;

  @override
  void initState() {
    _notifier = context.read<ScreenChangeNotifier>();
    super.initState();
  }

  @override
  void dispose() {
    _notifier.hello();
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      body: Container(),
    );
  }
}
  • Related