OpenFlutter / flutter_screenutil

Flutter screen adaptation, font adaptation, get screen information
https://pub.dartlang.org/packages/flutter_screenutil
Apache License 2.0
3.88k stars 494 forks source link

The following _CastError was thrown building Builder(dirty, dependencies: [MediaQuery]): Null check operator used on a null value #375

Closed alizestbrains closed 2 years ago

alizestbrains commented 2 years ago

My project is working fine until tomorrow. And now I get this error in main.dart

Null check operator used on a null value

The relevant error-causing widget was: 
  Builder Builder:file:///Users/mac_6/Documents/FlutterSDK/flutter/.pub-cache/hosted/pub.dartlang.org/flutter_screenutil-5.4.0+1/lib/src/screenutil_init.dart:29:14
When the exception was thrown, this was the stack: 
#0      _MyAppState._redirectToScreen (package:tabela_app/main.dart:356:28)
#1      _MyAppState.build.<anonymous closure> (package:tabela_app/main.dart:344:19)
#2      ScreenUtilInit.build.<anonymous closure> (package:flutter_screenutil/src/screenutil_init.dart:43:23)
#3      Builder.build (package:flutter/src/widgets/basic.dart:7398:48)
#4      StatelessElement.build (package:flutter/src/widgets/framework.dart:4827:28)
#5      ComponentElement.performRebuild (package:flutter/src/widgets/framework.dart:4754:15)
#6      Element.rebuild (package:flutter/src/widgets/framework.dart:4477:5)
#7      ComponentElement._firstBuild (package:flutter/src/widgets/framework.dart:4735:5)

Main.dart

ScreenUtilInit(
          minTextAdapt: true,
          splitScreenMode: true,
          builder: (_) => MaterialApp(
            navigatorKey: navigatorKey,
            title: 'Flutter Demo',
            locale: _locale,
            builder: (context, widget) {
              //add this line
              ScreenUtil.setContext(context);
              return MediaQuery(
                //Setting font does not change with system font size
                data: MediaQuery.of(context).copyWith(textScaleFactor: 1.0),
                child: widget!,
              );
            },
            theme: ThemeData(
              primarySwatch: Constants.appColor,
              textSelectionTheme: const TextSelectionThemeData(
                cursorColor: Colors.black,
                // selectionColor: Colors.black,
                selectionHandleColor: Colors.black,
              ),
              inputDecorationTheme: const InputDecorationTheme(
                border: OutlineInputBorder(
                  borderRadius: BorderRadius.all(Radius.circular(4.00)),
                ),
                fillColor: Colors.white,
                contentPadding: EdgeInsets.symmetric(
                  vertical: 12.50,
                  horizontal: 10.00,
                ),
              ),
            ),
            home: _redirectToScreen(),
            debugShowCheckedModeBanner: false,
          ),
        ),

So for that I update new version of Flutter ScreenUtil flutter_screenutil: ^5.4.0+1 and get again same error. What is the permanent solution for this error?. I notice whenever new version is available for screenutil then I face **Null check operator used on a null value**.

Joao-VictorF commented 2 years ago

+1

I did some testing and in my case, this error is happening when I have setStates in my initState function. To be more precisely, when I call a API and set the response value in previous defined variables, so the setState call my build method again and by some reason this error happens. If I comment the setStates inside my initState, everything works well.

So the error is that when my widget tree is rebuilded, at some moment, the widget is null.

Joao-VictorF commented 2 years ago

I found that the "Null check operator" error is being threw by the line 3088 of this PR

Maybe this can help them fix this issue as soon as possible.

Thanks!

image

jonchui commented 2 years ago

I'm getting a similar _CastError exception thrown too, when calling 100.w

_CastError (Null check operator used on a null value)

image
jonchui commented 2 years ago

@lizhuoyuan why was this closed? What's the solution?

Mounir-Bouaiche commented 2 years ago

@alizestbrains @Joao-VictorF @jonchui , please tell us which method you are using, ScreenUtil.init() or ScreenUtilInit().

alizestbrains commented 2 years ago

@Mounir-Bouaiche ScreenUtilInit().

alizestbrains commented 2 years ago

@lizhuoyuan why closing this issue without any solution?

jonchui commented 2 years ago

@alizestbrains @Joao-VictorF @jonchui , please tell us which method you are using, ScreenUtil.init() or ScreenUtilInit().

I'm using ScreenUtil.init() @Mounir-Bouaiche

@lizhuoyuan why closing this issue without any solution?

same question for me? It's still throwing Another exception was thrown: Null check operator used on a null value for me and showing a red page

image
Joao-VictorF commented 2 years ago

@alizestbrains @Joao-VictorF @jonchui , please tell us which method you are using, ScreenUtil.init() or ScreenUtilInit().

I was able to solve my issue by looking in this question of @alizestbrains. I was actually using both, and it was working well, as my app is not new. Maybe this issue was included in the last updates.

So, my main.dart was somenthing like this:

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

  @override
  Widget build(BuildContext context) {
    return StyledToast(
      locale: const Locale('pt', 'BR'),
      child: ScreenUtilInit(
        designSize: const Size(375, 812),
        minTextAdapt: true,
        splitScreenMode: true,
        builder: (_) => MaterialApp(
          title: 'Futebolês',
          routes: routes,
          theme: theme(context),
          home: const AppHome(),
          debugShowCheckedModeBanner: false,
        )
      )
    );
  }
}

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

  @override
  Widget build(BuildContext context) {
    SizeConfig().init(context);
    // ScreenUtil.init(context); <-- Here
    // ScreenUtil.setContext(context);
    return  UpgradeAlert(
      countryCode: 'BR',
      messages: UpgraderMessages(code: 'pt'),
      child: const FutebolesHomeScreen()
    );
  }
}

After I commented these 2 lines in the AppHome, everything is working as expected for now, I have to do more tests 'cause my app have a lot of pages, but for now it's working.

Maybe you guys should verify if you're using ScreenUtil.init() inside a ScreenUtilinit widget?

Thanks!

jonchui commented 2 years ago

I found that the "Null check operator" error is being threw by the line 3088 of this PR

Maybe this can help them fix this issue as soon as possible.

Thanks!

I can confirm this is where my crash is coming from too!

Mounir-Bouaiche commented 2 years ago

@jonchui @Joao-VictorF , thanks guys, this error was previously without explanation or log. Anyway, a fix should be ready by next 48H.

Mounir-Bouaiche commented 2 years ago

And I be grateful if someone just contact me to test changes earlier.

jonchui commented 2 years ago

What’s the best way to contact you? Via an issue on GitHub?

Jon Chui Partner @ Wonder and Wander

WonderAndWander.io ( http://wonderandwander.io/ )

"Helping mission-driven companies amplify their impact with Technology. Book a Free Consult ( https://wonderandwander.io/talk-to-us ) "

Sent via Superhuman iOS ( @.*** )

On Mon, Apr 25 2022 at 5:34 PM, Mounir Bouaiche < @.*** > wrote:

And I be grateful if someone just contact me to test changes earlier.

— Reply to this email directly, view it on GitHub ( https://github.com/OpenFlutter/flutter_screenutil/issues/375#issuecomment-1109140001 ) , or unsubscribe ( https://github.com/notifications/unsubscribe-auth/AABSARESZ5M4IXBS26F6YZ3VG4MV7ANCNFSM5UBFWQ3Q ). You are receiving this because you were mentioned. Message ID: <OpenFlutter/flutter_screenutil/issues/375/1109140001 @ github. com>

Mounir-Bouaiche commented 2 years ago

@jonchui thanks for your very quick reply, I didn't expect that. Please, could you open new issue ?

jonchui commented 2 years ago

@jonchui @Joao-VictorF , thanks guys, this error was previously without explanation or log. Anyway, a fix should be ready by next 48H.

I'm confused. So you will fix this in 48H, but you want me to still open a new GitHub issue for this null error?

Mounir-Bouaiche commented 2 years ago

@jonchui That was your suggestion after all. Personally, I recommend contacting via email. I just thought you wouldn't give me your email. Btw, my email: bouaiche.9@gmail.com

jonchui commented 2 years ago

Ah! Okay. I’m @.***

I’m just wondering how to solve this crash. what can I do to help?

Or are you solving it already?

Jon Chui Partner @ Wonder and Wander

WonderAndWander.io ( http://wonderandwander.io/ )

"Helping mission-driven companies amplify their impact with Technology. Book a Free Consult ( https://wonderandwander.io/talk-to-us ) "

Sent via Superhuman iOS ( @.*** )

On Mon, Apr 25 2022 at 6:45 PM, Mounir Bouaiche < @.*** > wrote:

@jonchui ( https://github.com/jonchui ) That was your suggestion after all. Personally, I recommend contacting via email. I just thought you wouldn't give me your email. Btw, my email: @.***

— Reply to this email directly, view it on GitHub ( https://github.com/OpenFlutter/flutter_screenutil/issues/375#issuecomment-1109177673 ) , or unsubscribe ( https://github.com/notifications/unsubscribe-auth/AABSARFTS2SQO5ZDNXEO3UTVG4VDLANCNFSM5UBFWQ3Q ). You are receiving this because you were mentioned. Message ID: <OpenFlutter/flutter_screenutil/issues/375/1109177673 @ github. com>

Mounir-Bouaiche commented 2 years ago

Well, I already have an idea, and I need someone who is facing this error to confirm that the issue is fixed, so I could reach him to test and maybe to share his screen with me if necessary.

jonchui commented 2 years ago

Ahh I see! Yes open a PR and I can pull it to confirm?

Jon Chui Partner @ Wonder and Wander

WonderAndWander.io ( http://wonderandwander.io/ )

"Helping mission-driven companies amplify their impact with Technology. Book a Free Consult ( https://wonderandwander.io/talk-to-us ) "

Sent via Superhuman iOS ( @.*** )

On Mon, Apr 25 2022 at 6:57 PM, Mounir Bouaiche < @.*** > wrote:

Well, I already have an idea, and I need someone who is facing this error to confirm that the issue is fixed, so I could reach him to test and maybe to share his screen with me if necessary.

— Reply to this email directly, view it on GitHub ( https://github.com/OpenFlutter/flutter_screenutil/issues/375#issuecomment-1109184364 ) , or unsubscribe ( https://github.com/notifications/unsubscribe-auth/AABSARDXYQ5LOKOCE6UGBRLVG4WNVANCNFSM5UBFWQ3Q ). You are receiving this because you were mentioned. Message ID: <OpenFlutter/flutter_screenutil/issues/375/1109184364 @ github. com>

Mounir-Bouaiche commented 2 years ago

Hello @jonchui @alizestbrains , may I ask you a few questions, if you don't mind, : 1- Is error shown in the page where you have used ScreenUtil.init() or elsewhere ? 2- I want you to use ScreenUtil.setContext(context) on every page in the app. Do you still getting this error ? 3- Is error shown in a particular page or choses where to show up randomly ? 4- Are you using both ScreenUtil.init() and ScreenUtilInit() ? 5- Do you use one of these methods: Navigator.of(context).pushAndRemoveUntil() or pushNamedAndRemoveUntil() ? if yes, did error show up when you use library after calling one of them ?

Hello @Joao-VictorF , thanks for your help mate, but I have to ask, did error still showing up after removing all ScreenUtil.init() ?

lizhuoyuan commented 2 years ago

Sorry I only had time to see this question, please give me some time to look carefully at the problem and think about the solution. It would be nice to be able to provide a reproducible example for me to run

jonchui commented 2 years ago

1- Is error shown in the page where you have used ScreenUtil.init() or elsewhere ?

Yeah just 4 pages

2- I want you to use ScreenUtil.setContext(context) on every page in the app. Did you still getting this error ?

This fixed it! I put this at the top of the build function for each of those pages.... Wow, why was that so easy?!?

3- Is error shown in a particular page or choses where to show up randomly ? Yeah those pages - though sometimes, i found the "home page" also showed up "randomly" when i came backwards.

Then i saw in the codebase for the "home page"

          child: Obx(
            () => _controller.isLoading.value
                ? _buildLoading()
                : _buildBody(context),

And then went into the _buildBody(context) function and called ScreenUtil.setContext(context)

that seemed to fix it

4- Are you using both ScreenUtil.init() and ScreenUtilInit() ?

Only using ScreenUtil.init()

5- Do you use one of these methods: Navigator.of(context).pushAndRemoveUntil() or pushNamedAndRemoveUntil() ?

if yes, did error show up when you use library after calling one of them ? Do not use those

Sorry I only had time to see this question, please give me some time to look carefully at the problem and think about the solution. It would be nice to be able to provide a reproducible example for me to run

@lizhuoyuan - do you have any idea why the above solution fixes it? Is is just that the new framework.dart code in this PR causes us to need to set the Context each time we build?!?

(seems wasteful)

lizhuoyuan commented 2 years ago

@jonchui
Based on the current performance, I guess that the value of the context is null, but because I have not encountered such a situation, I am not sure what is the situation and when the context is null, so sad

Mounir-Bouaiche commented 2 years ago

@jonchui Ok mate, 1- replace in your pubspec.yaml this code:

flutter_screenutil:
    git:
      url: https://github.com/Mounir-Bouaiche/flutter_screenutil
      branch: master

2- Use ScreenUtil.init() only once, if you can, in the app and remove all ScreenUtil.setContext(context) (will be removed in next releases), and let me know the result.

Mounir-Bouaiche commented 2 years ago

@lizhuoyuan Hello friend, I knew what is the problem. This problem happens when you try to reach the BuildContext as in MediaQuery.of(context) but widget related was unmounted, the widget that uses ScreenUtil.init(). I managed to fix it, so my PR will be available by the end of the day when someone confirm that my solution is working.

Mounir-Bouaiche commented 2 years ago

@lizhuoyuan I can give you a code that demonstrate this problem if you want ?

lizhuoyuan commented 2 years ago

@lizhuoyuan I can give you a code that demonstrate this problem if you want ?

ok , ty. My previous thought was that when the corresponding components are uninstalled and the context is empty, they should not be displayed. In theory, they will disappear together.

Mounir-Bouaiche commented 2 years ago

@lizhuoyuan this code will show you why this error show up, and I use it to debug and fix, so when someone confirm that is working by the end of the day I will open a PR:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() => runApp(MaterialApp(home: ThirdPage()));

class MyStatelessElement<T extends TestPage> extends StatelessElement {
  MyStatelessElement(T widget) : super(widget);

  @override
  T get widget => super.widget as T;

  @override
  void mount(Element? parent, Object? newSlot) {
    super.mount(parent, newSlot);
    print('${widget.text()} is mounted');
  }

  @override
  void unmount() {
    print('${widget.text()} is unmounted');
    super.unmount();
  }
}

abstract class TestPage extends StatelessWidget {
  String text();

  Widget goto();

  @override
  StatelessElement createElement() => MyStatelessElement(this);

  @override
  Widget build(BuildContext context) {
    Timer(const Duration(seconds: 3), () {
      Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (_) => goto()),
        (route) => false,
      );
    });
    return Scaffold(
      body: Center(
        child: Text(
          text(),
          style: TextStyle(fontWeight: FontWeight.w900, fontSize: 32.sp),
        ),
      ),
    );
  }
}

class FirstPage extends TestPage {
  @override
  String text() => 'First Page';

  @override
  Widget goto() => SecondPage();
}

class SecondPage extends TestPage {
  @override
  String text() => 'Second Page';

  @override
  Widget goto() => FirstPage();
}

class ThirdPage extends TestPage {
  @override
  String text() => 'Third Page';

  @override
  Widget goto() => FirstPage();

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context);
    return super.build(context);
  }
}
lizhuoyuan commented 2 years ago

@Mounir-Bouaiche If you want, I hope I can invite you as a publisher to jointly maintain flutter_screenutil

lizhuoyuan commented 2 years ago

@lizhuoyuan this code will show you why this error show up, and I use it to debug and fix, so when someone confirm that is working by the end of the day I will open a PR:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() => runApp(MaterialApp(home: ThirdPage()));

class MyStatelessElement<T extends TestPage> extends StatelessElement {
  MyStatelessElement(T widget) : super(widget);

  @override
  T get widget => super.widget as T;

  @override
  void mount(Element? parent, Object? newSlot) {
    super.mount(parent, newSlot);
    print('${widget.text()} is mounted');
  }

  @override
  void unmount() {
    print('${widget.text()} is unmounted');
    super.unmount();
  }
}

abstract class TestPage extends StatelessWidget {
  String text();

  Widget goto();

  @override
  StatelessElement createElement() => MyStatelessElement(this);

  @override
  Widget build(BuildContext context) {
    Timer(const Duration(seconds: 3), () {
      Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (_) => goto()),
        (route) => false,
      );
    });
    return Scaffold(
      body: Center(
        child: Text(
          text(),
          style: TextStyle(fontWeight: FontWeight.w900, fontSize: 32.sp),
        ),
      ),
    );
  }
}

class FirstPage extends TestPage {
  @override
  String text() => 'First Page';

  @override
  Widget goto() => SecondPage();
}

class SecondPage extends TestPage {
  @override
  String text() => 'Second Page';

  @override
  Widget goto() => FirstPage();
}

class ThirdPage extends TestPage {
  @override
  String text() => 'Third Page';

  @override
  Widget goto() => FirstPage();

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context);
    return super.build(context);
  }
}

If you just use getElementForInheritedWidgetOfExactType, there will still be a warning, but there will be no crash

Mounir-Bouaiche commented 2 years ago

@Mounir-Bouaiche If you want, I hope I can invite you as a publisher to jointly maintain flutter_screenutil

I'll be grateful then

Mounir-Bouaiche commented 2 years ago

@lizhuoyuan this code will show you why this error show up, and I use it to debug and fix, so when someone confirm that is working by the end of the day I will open a PR:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() => runApp(MaterialApp(home: ThirdPage()));

class MyStatelessElement<T extends TestPage> extends StatelessElement {
  MyStatelessElement(T widget) : super(widget);

  @override
  T get widget => super.widget as T;

  @override
  void mount(Element? parent, Object? newSlot) {
    super.mount(parent, newSlot);
    print('${widget.text()} is mounted');
  }

  @override
  void unmount() {
    print('${widget.text()} is unmounted');
    super.unmount();
  }
}

abstract class TestPage extends StatelessWidget {
  String text();

  Widget goto();

  @override
  StatelessElement createElement() => MyStatelessElement(this);

  @override
  Widget build(BuildContext context) {
    Timer(const Duration(seconds: 3), () {
      Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (_) => goto()),
        (route) => false,
      );
    });
    return Scaffold(
      body: Center(
        child: Text(
          text(),
          style: TextStyle(fontWeight: FontWeight.w900, fontSize: 32.sp),
        ),
      ),
    );
  }
}

class FirstPage extends TestPage {
  @override
  String text() => 'First Page';

  @override
  Widget goto() => SecondPage();
}

class SecondPage extends TestPage {
  @override
  String text() => 'Second Page';

  @override
  Widget goto() => FirstPage();
}

class ThirdPage extends TestPage {
  @override
  String text() => 'Third Page';

  @override
  Widget goto() => FirstPage();

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context);
    return super.build(context);
  }
}

If you just use getElementForInheritedWidgetOfExactType, there will still be a warning, but there will be no crash

Ah I see, could you let me know what warning is?

lizhuoyuan commented 2 years ago

@lizhuoyuan this code will show you why this error show up, and I use it to debug and fix, so when someone confirm that is working by the end of the day I will open a PR:

import 'dart:async';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

void main() => runApp(MaterialApp(home: ThirdPage()));

class MyStatelessElement<T extends TestPage> extends StatelessElement {
  MyStatelessElement(T widget) : super(widget);

  @override
  T get widget => super.widget as T;

  @override
  void mount(Element? parent, Object? newSlot) {
    super.mount(parent, newSlot);
    print('${widget.text()} is mounted');
  }

  @override
  void unmount() {
    print('${widget.text()} is unmounted');
    super.unmount();
  }
}

abstract class TestPage extends StatelessWidget {
  String text();

  Widget goto();

  @override
  StatelessElement createElement() => MyStatelessElement(this);

  @override
  Widget build(BuildContext context) {
    Timer(const Duration(seconds: 3), () {
      Navigator.of(context).pushAndRemoveUntil(
        MaterialPageRoute(builder: (_) => goto()),
        (route) => false,
      );
    });
    return Scaffold(
      body: Center(
        child: Text(
          text(),
          style: TextStyle(fontWeight: FontWeight.w900, fontSize: 32.sp),
        ),
      ),
    );
  }
}

class FirstPage extends TestPage {
  @override
  String text() => 'First Page';

  @override
  Widget goto() => SecondPage();
}

class SecondPage extends TestPage {
  @override
  String text() => 'Second Page';

  @override
  Widget goto() => FirstPage();
}

class ThirdPage extends TestPage {
  @override
  String text() => 'Third Page';

  @override
  Widget goto() => FirstPage();

  @override
  Widget build(BuildContext context) {
    ScreenUtil.init(context);
    return super.build(context);
  }
}

If you just use getElementForInheritedWidgetOfExactType, there will still be a warning, but there will be no crash

Ah I see, could you let me know what warning is?

======== 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      StatelessElement.widget (package:flutter/src/widgets/framework.dart:4824:39)
#2      MyStatelessElement.widget (package:example/main.dart:11:25)
#3      MyStatelessElement.unmount (package:example/main.dart:22:14)
#4      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1926:13)
#5      _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#6      SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#7      _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#8      _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#9      ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#10     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#11     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#12     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#13     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#14     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#15     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#16     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#17     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#18     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#19     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#20     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#21     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#22     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#23     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#24     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#25     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#26     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#27     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#28     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#29     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#30     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#31     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#32     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#33     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#34     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#35     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#36     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#37     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#38     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#39     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#40     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#41     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#42     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#43     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#44     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#45     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#46     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#47     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#48     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#49     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#50     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#51     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#52     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#53     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#54     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#55     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#56     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#57     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#58     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#59     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#60     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#61     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#62     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#63     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#64     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#65     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#66     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#67     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#68     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#69     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#70     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#71     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#72     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#73     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#74     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#75     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#76     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#77     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#78     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#79     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#80     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#81     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#82     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#83     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#84     SingleChildRenderObjectElement.visitChildren (package:flutter/src/widgets/framework.dart:6271:14)
#85     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#86     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#87     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#88     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#89     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#90     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#91     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#92     _InactiveElements._unmount.<anonymous closure> (package:flutter/src/widgets/framework.dart:1924:7)
#93     ComponentElement.visitChildren (package:flutter/src/widgets/framework.dart:4807:14)
#94     _InactiveElements._unmount (package:flutter/src/widgets/framework.dart:1922:13)
#95     ListIterable.forEach (dart:_internal/iterable.dart:39:13)
#96     _InactiveElements._unmountAll (package:flutter/src/widgets/framework.dart:1935:25)
#97     BuildOwner.lockState (package:flutter/src/widgets/framework.dart:2519:15)
#98     BuildOwner.finalizeTree (package:flutter/src/widgets/framework.dart:2932:7)
#99     WidgetsBinding.drawFrame (package:flutter/src/widgets/binding.dart:884:19)
#100    RendererBinding._handlePersistentFrameCallback (package:flutter/src/rendering/binding.dart:363:5)
#101    SchedulerBinding._invokeFrameCallback (package:flutter/src/scheduler/binding.dart:1144:15)
#102    SchedulerBinding.handleDrawFrame (package:flutter/src/scheduler/binding.dart:1081:9)
#103    SchedulerBinding._handleDrawFrame (package:flutter/src/scheduler/binding.dart:995:5)
#107    _invoke (dart:ui/hooks.dart:151:10)
#108    PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#109    _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)
====================================================================================================
Application finished.
Mounir-Bouaiche commented 2 years ago

@lizhuoyuan copy the code, again, I've modified, and use my forked library. I did a mistake when copying code in first time.

lizhuoyuan commented 2 years ago

@lizhuoyuan copy the code, again, I've modified, and use my forked library. I did a mistake when copying code in first time.

nice,bro.

Mounir-Bouaiche commented 2 years ago

@lizhuoyuan copy the code, again, I've modified, and use my forked library. I did a mistake when copying code in first time.

nice,bro.

Did warning disappear ?

lizhuoyuan commented 2 years ago

@lizhuoyuan copy the code, again, I've modified, and use my forked library. I did a mistake when copying code in first time.

nice,bro.

Did warning disappear ?

yes !

Mounir-Bouaiche commented 2 years ago

@lizhuoyuan copy the code, again, I've modified, and use my forked library. I did a mistake when copying code in first time.

nice,bro.

Did warning disappear ?

yes !

Good to hear, hahaha you scared me a little.

Mounir-Bouaiche commented 2 years ago

This is a duplication of #362

jonchui commented 2 years ago

Hmm @Mounir-Bouaiche - when i took out my extra code that called ScreenUtil.setContext everywhere and just went back to calling:

ScreenUtil.init(context, designSize: Size(Get.width, Get.height));

In the 3 places @ the top of the Widget build(BuildContext context) function

.. I'm getting the same error still, even with the new 5.5.1

Am i missing something basic?>

Should i only be calling ScreenUtil.init in one place, not in the 3 screens we have (since we have a three screened' tab bar?)

Here's the crash:

════════ Exception caught by widgets library ═══════════════════════════════════
The following _CastError was thrown building Obx(has builder, dirty, state: _ObxState#754d0):
Null check operator used on a null value

The relevant error-causing widget was
Obx
package:aureoapp/…/home/home_view.dart:55
When the exception was thrown, this was the stack
#0      Element.widget
package:flutter/…/widgets/framework.dart:3270
#1      StatelessElement.widget
package:flutter/…/widgets/framework.dart:4937
#2      debugCheckHasMediaQuery.<anonymous closure>
package:flutter/…/widgets/debug.dart:229
#3      debugCheckHasMediaQuery
package:flutter/…/widgets/debug.dart:245
#4      MediaQuery.of
package:flutter/…/widgets/media_query.dart:859
#5      ScreenUtil.screenHeight
package:flutter_screenutil/src/screen_util.dart:143
#6      ScreenUtil.scaleHeight
package:flutter_screenutil/src/screen_util.dart:161
#7      ScreenUtil.setHeight
package:flutter_screenutil/src/screen_util.dart:182
#8      SizeExtension.h
package:flutter_screenutil/src/size_extension.dart:12
#9      HomeView._buildBody
package:aureoapp/…/home/home_view.dart:112
#10     HomeView.build.<anonymous closure>
package:aureoapp/…/home/home_view.dart:56
#11     Obx.build
package:get/…/rx_flutter/rx_obx_widget.dart:70
#12     RxInterface.notifyChildren
package:get/…/rx_core/rx_interface.dart:26
#13     _ObxState.build
package:get/…/rx_flutter/rx_obx_widget.dart:54
#14     StatefulElement.build
package:flutter/…/widgets/framework.dart:4984
#15     ComponentElement.performRebuild
package:flutter/…/widgets/framework.dart:4870
#16     StatefulElement.performRebuild
package:flutter/…/widgets/framework.dart:5043
#17     Element.rebuild
package:flutter/…/widgets/framework.dart:4588
#18     BuildOwner.buildScope
package:flutter/…/widgets/framework.dart:2691
#19     WidgetsBinding.drawFrame
package:flutter/…/widgets/binding.dart:882
#20     RendererBinding._handlePersistentFrameCallback
package:flutter/…/rendering/binding.dart:363
#21     SchedulerBinding._invokeFrameCallback
package:flutter/…/scheduler/binding.dart:1144
#22     SchedulerBinding.handleDrawFrame
package:flutter/…/scheduler/binding.dart:1081
#23     SchedulerBinding._handleDrawFrame
package:flutter/…/scheduler/binding.dart:995
#27     _invoke (dart:ui/hooks.dart:151:10)
#28     PlatformDispatcher._drawFrame (dart:ui/platform_dispatcher.dart:308:5)
#29     _drawFrame (dart:ui/hooks.dart:115:31)
(elided 3 frames from dart:async)

The init is getting called here in 3 different screens:

image

Mounir-Bouaiche commented 2 years ago

I've updated the library and the error is fixed. Go check it out.

jonchui commented 2 years ago

We’re using “ScreenUtil.init()”

Does switching over fix the null error?

Jon Chui Partner @ Wonder and Wander

WonderAndWander.io ( http://wonderandwander.io/ )

"Helping mission-driven companies amplify their impact with Technology. Book a Free Consult ( https://wonderandwander.io/talk-to-us ) "

Sent via Superhuman iOS ( @.*** )

On Sun, Apr 24 2022 at 7:32 AM, Mounir Bouaiche < @.*** > wrote:

@alizestbrains ( https://github.com/alizestbrains ) @Joao-VictorF ( https://github.com/Joao-VictorF ) @jonchui ( https://github.com/jonchui ) , please tell us which method you are using, ScreenUtil.init() or ScreenUtilInit().

— Reply to this email directly, view it on GitHub ( https://github.com/OpenFlutter/flutter_screenutil/issues/375#issuecomment-1107842978 ) , or unsubscribe ( https://github.com/notifications/unsubscribe-auth/AABSAREELSVPFYGERP2DVYTVGU5PXANCNFSM5UBFWQ3Q ). You are receiving this because you were mentioned. Message ID: <OpenFlutter/flutter_screenutil/issues/375/1107842978 @ github. com>